Using JavaBeans


JavaBeans are a standardized format for Java classes. Basically, they define a pattern that is used when creating a class; this pattern allows for fast and easy inspection of the elements and methods of the class. It is important to understand that JavaBeans are not directly related to Enterprise JavaBeans (EJBs), other than that they are both Java component models. JavaBeans are Java classes that follow a simple design pattern:

  • They have at least a no argument constructor.

  • They have get and set methods for all their member variables that are named the same as the variables except that the first letter is capitalized. For example, if a JavaBean has a member variable named firstName , it also must have a getFirstName() method and a setFirstName() method.

Instantiating JavaBeans in JSP

JavaBeans can be used easily in a JSP to help improve reusability of the beans without embedding large amounts of Java code into the JSP. The first step in using a bean in a JSP is to create it using the JSP directive:

 
 <jsp:useBean id="sample" class="com.xigole.examples.Examples" /> 

This directive tells the JSP compiler to instantiate an object of type "com.xigole.examples.Example" and assign it to the variable named "sample" .

Manipulating JavaBean Properties in a JSP

After you create a JavaBean, you can get and set the properties of the bean in two different ways. The first is to use XML syntax:

 
 <jsp:setProperty name ="sample" property="sampleProperty" value="This is the sample property" /> 

Likewise, to get properties from a bean, you use the following:

 
 <b>The value of the sampleProperty is <jsp:getProperty name="sample" property="sampleProperty" /></b> 

The other method of manipulating bean properties is to access the bean through standard JSP tags, using the name of the bean. For example:

 
 <b>The value of the sampleProperty is <%= sample.getSampleProperty() %></b> 

Setting Bean Properties from the Request

A common usage of beans is to package the variables of the HttpServletRequest for easy manipulation. One way to do this is to include a call to request.getParameter() in the <jsp:setproperty> call. Normally, you would use code like this:

 
 <jsp:setProperty name="sample" property="sampleProperty" value='<%= request.getParmeter("sampleProperty") %>' /> 

Notice in this example the use of both single quotation marks and double quotation marks. Using them both is required so that the JSP compiler can tell which quotation marks belong to which part of the statement. Attribute values can be enclosed in single or double quotation marks, and we take advantage of that usage here to not confuse the JSP compiler.

JSP allows you to automatically set bean properties from the request as well. The syntax for setting them is

 
 <jsp:setProperty name="sample" property="*" /> 

This statement sets the property for all properties in the bean that have parameters with the same name. That is, there must be a parameter in the HttpServletRequest with the same name as the bean property. Any missing bean properties are silently ignored. Any additional parameters in the HttpServletRequest that are not used by the bean are silently ignored too. Again, you need to understand that the bean property names and the request parameter names must match exactly, including case, for this approach to work.

Creating the JavaBean Class

As mentioned before, the JavaBean class abides by certain design patterns to allow the JSP compiler to easily access the properties of the bean. The primary requirements are for the bean to have a no argument constructor and for the get and set methods to mirror the member variables. A sample bean might look like the one shown in Listing 18.6.

Listing 18.6 SampleJavaBean.java
 package jspexample; public class SampleJavaBean { private String sampleProperty; public SampleJavaBean() { } public void setSampleProperty( String property ) { sampleProperty = property; } public String getSampleProperty() { return( sampleProperty ); } } 

The bean has a single member variable named sampleProperty . There also are set and get methods for this member variable; in each case, the variable name has the first letter in uppercase and the rest of the variable name the same.

It is important to note that a JavaBean does not have to be just a simple container for known variable names. It is perfectly legal, and even encouraged, for a bean to have methods that are more complex. The sample bean can have a method to persist its data to a database. This method can be called from the JSP using scriptlets.

JavaBean Example

Now you can create a simple HTML form and a JavaBean to hold the data from the form. The JSP, named beanExample.jsp , looks like Listing 18.7.

Listing 18.7 beanExample.jsp
[View full width]
 <html> <head> <title>JavaBean form example</title> </head> <body> <form action="/myFirstWebApp/beanExample.jsp" method="get"> <b>JavaBean form example</b><br> <b>User name:</b> <input type="text" name="userName"><br> <b>Password:</b> <input type="password" name="password"><br> <b>Email Address:</b> <input type="text" name="email"><br> <input type="submit" value="Submit"> </form> <jsp:useBean id="example" class="jspexample.JavaBeanExample" /> <jsp:setProperty name="example" property="*" /> <% if( example.getUserName() != null ) // then we already did the set { %> <p><b>The user name entered is: </b><jsp:getProperty name="example" graphics/ccc.gif property="userName" /></p> <p><b>The password entered is: </b><jsp:getProperty name="example" graphics/ccc.gif property="password" /></p> <p><b>The email address entered is: </b><jsp:getProperty name="example" graphics/ccc.gif property="email" /></p> <% } %> </body> </html> 

The JavaBean, named JavaBeanExample.java in the src/jspexample directory, looks like Listing 18.8.

Listing 18.8 JavaBeanExample.java
 package jspexample; public class JavaBeanExample { private String userName; private String password; private String email; public JavaBeanExample() { } public void setUserName( String name ) { userName = name; } public String getUserName() { return( userName ); } public void setPassword( String pass ) { password = pass; } public String getPassword() { return( password ); } public void setEmail( String userEmail ) { email = userEmail; } public String getEmail() { return( email ); } } 

The first run with this JavaBean looks like Figure 18.4.

Figure 18.4. Output from the first run of the JavaBean example.

graphics/18fig04.gif

Then, after you enter values into the HTML form, the result looks like Figure 18.5.

Figure 18.5. Output from the second run of the JavaBean example.

graphics/18fig05.gif



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net