Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaBeans and JSP CS-422.

Similar presentations


Presentation on theme: "JavaBeans and JSP CS-422."— Presentation transcript:

1 JavaBeans and JSP CS-422

2 JavaBeans The Java component technology
originally intended for the creation and management of “pluggable” GUI components; Java’s answer to Visual Basic’s VBX/OCXs becoming more popular for encapsulating business logic for server-side applications (especially Java Server Pages many Java GUI Development tools have been modified to allow application development of server-side applications (Visual Café, Jbuilder, VisualAge for Java) along with the development tools being delivered with application servers (SilverStream, BEA Weblogic) A JavaBean is nothing more than a class that maintains some state data (called properties) and follows a certain set of coding conventions. Along with certain Java runtime support (reflection and introspection) can JavaBeans can be easily added to and maintained by most of the Java GUI Development Tools .

3 API requires that: Must implement java.io.Serializable or java.io.Externalizable Beans must be able to support their own persistence this allows the bean to be saved and restored consistently provide a no-arguments constructor provides a single way for the Bean to be instantiated insures consistent bean creation and initialization private properties must have corresponding get/set methods that follow the appropriate naming patterns each piece of state data to be exposed is called a property made public via accessor and mutators (setters and getters) accessor methos names must start with “get” for property int color the accessor would be getcolor() mutator methods names must start with “set” for property fuelCapacity the mutator would be setfuelCapacity()

4 Environmental Support
To use JavaBeans with JSP no environmental support is needed, but for a better understanding of JavaBeans in the whole we’ll take a look… Introspection ( java.beans.Introspector) defines the resources that allow the programatic construction of information on the Bean facilitates the discovery of properties and methods by inspecting the .class file can be done two ways : the reflection APIs or by providing a java.beans.BeanInfo class with the bean to describe the public methods and properties, Reflection (java.lang.reflect) a set of resources that allows a program to discover the public properties and methods of classes that have been loaded by the class loader this is facilitated by the naming convention for accessors and mutators (i.e. the names must start with “get” or “set”) don’t need to need to knownames of properties as long as we know the names of the accessor and mutators These environmental support mechanisma allow the JavaBeans to be cleanly integrated into GUI based, Java Development tools like VisualCafe, VisualAge and Jbuilder.

5 Example: a Stock bean import java.io.Serializable;
public class Stock implements Serializable { Stock( ) { } // no argument constructor (just creates the object) private String name = null; private String tickerSymbol = null; private double tradingPrice = 0.0; private double sharesOwned = 0.0; public void setName(String n) { name = n; } public String getName( ) { return name;} public void setTickerSymbol( String s ) { tickerSymbol = s; } public String gettickerSymbol( ) { return tickerSymbol; } public void settradingPrice( double d ) { tradingPrice = d; } public double gettradingPrice( ) { return tradingPrice; } public void setsharesOwned( double v ) { sharesOwned = v; } public double getsharesOwned( ) { return sharesOwned; } }

6 jsp:useBean Tag jsp:useBean does the following:
If the object is found within the specified scope it is retrieved and assigned to the object if not found it is instantiated if newly instantiated it executes the code specified in the body ( one or more jsp:setProperty tags or a scriptlet) if newly instantiated it is saved into the scope via setAttribute( ) method jsp:useBean also makes the bean visible to the JSP; there may be other objects in the context that were put there by other JSPs or servlets; jsp:useBean can make them visible to the current JSP

7 jsp:useBean Tag (more)
Attributes : id scope class beanName type <jsp:useBean id = today class = “java.util.Date”> instantiates a bean called today of class java.util.Date( ) <jsp:useBean id = “count” class = “java.lang.Integer” type = “java.lang.Number”> essentially does : Number = count ; count = new Integer( ) <jsp:useBean id = “count” class = “<%= request.getParameter(“beanName”)%> type = “Number” /> essentiallt does: Number count; count = java.beans.Beans.instantiate(request.getParameter(“beanName”));

8 jsp:useBean Tag (more)
In general the syntax is: <jsp:useBean id = “name” {scope = “page | request | session | application”} { class = “class name” | class = “class name” type = “type name” | beanName = “bean name” type = “type name” | type = “type name” } />


Download ppt "JavaBeans and JSP CS-422."

Similar presentations


Ads by Google