Sei sulla pagina 1di 6

The CalculatorBean

package com.brainysoftware; public class CalculatorBean { public int doubleIt(int number) { return 2 * number; } }

Calling a Bean from Your JSP Page


<jsp:useBean id="theBean" class="com.brainysoftware.CalculatorBean"/> <HTML> <HEAD> </HEAD> <BODY> <% int i = 4; int j = theBean.doubleIt(i); out.print("2*4=" + j); %> </BODY> </HTML>

Making a Bean Available

Before you can use a bean in your JSP page, you must make the bean available, using the jsp:useBean action element. This element has attributes that you can use to control the bean's behavior. The syntax for the jsp:useBean element has two forms. The first form is used when you don't need to write any initialization code, and the second form is used if you have Java code that needs to be run when the bean initializes. Code initialization is discussed in the next section. The two forms of the jsp:useBean action element are as follows:
<jsp:useBean (attribute="value")+/>

and
<jsp:useBean (attribute="value")+> initialization code </jsp:useBean>

The (attribute="value")+ part of the code means that one or more attributes must be present. The five attributes that can be used in a jsp:useBean action element are as follows:

id class type scope beanName

Either the class attribute or the type attribute must be present. The five attributes are explained in the following sections.
id

The id attribute defines a unique identifier for the bean. This identifier can be used throughout the page and can be thought of as the object reference for the bean. The value for the id attribute has the same requirements as a valid variable name in the current scripting language.
class

The class attribute specifies the fully qualified name for the JavaBean class. A fully qualified name is not required if the bean's package is imported using the page directive, however.
type

If the type attribute is present in a jsp:useBean element, it specifies the type of the JavaBean class. The type of the bean could be the type of the class itself, the type of its superclass, or an interface the bean class implements. Normally, this attribute isn't often used.
scope

The scope attribute defines the accessibility and the life span of the bean. This attribute can take one of the following values:

page request session application

The default value of the scope attribute is page. The scope is a powerful feature that lets you control how long the bean will continue to exist. Each of the attribute values is discussed in the following sections.
page

Using the page attribute value, the bean is available only in the current page after the point where the jsp:useBean action element is used. A new instance of the bean will be instantiated every

time the page is requested. The bean will be automatically destroyed after the JSP page loses its scope; namely, when the control moves to another page. If you use the jsp:include or jsp:forward tags, the bean will not be accessible from the included or forwarded page.
request

With the request attribute value, the accessibility of the bean is extended to the forwarded or included page referenced by a jsp:forward or jsp:include action element. The forwarded or included page can use the bean without having a jsp:useBean action element. For example, from inside a forwarded or included page, you can use the jsp:getProperty and jsp:setProperty action elements that reference to the bean instantiated in the original page.
session

A bean with a session scope is placed in the user's session object. The instance of the bean will continue to exist as long as the user's session object exists. In other words, the bean's accessibility extends to other pages. Because the bean's instance is put in the session object, you cannot use this scope if the page on which the bean is instantiated does not participate in the JSP container's session management. For example, the following code will generate an error:
<%@ page session="false" %> <jsp:useBean id="theBean" scope="session" class="com.brainysoftware.CalculatorBean"/> application

A bean with an application scope lives throughout the life of the JSP container itself. It is available from any page in the application.
beanName

The beanName attribute represents a name for the bean that the instantiate method of the java.beans.Beans class expects. As an example, the following is a jsp:useBean that instantiates the bean called com.newriders.HomeLoanBean:
<jsp:useBean id="theBean" class="com.newriders.HomeLoanBean"/>

Alternatively, you can import the package com.newriders using a page directive and refer to the class using its name, as follows:
<%@ page import="com.newriders" %> <jsp:useBean id="theBean" class="HomeLoanBean"/>

Note

The bean is available in the page after the jsp:useBean action element. It is not available before that point.

Consider the bean called CalculatorBean in the com.brainysoftware package in Listing 10.3. It has a private integer called memory. (Note that the variable name starts with a lowercase m.) It also has a getter called getMemory and a setter named setMemory. Note that in both accessor methods, memory is spelled using an uppercase M.
Listing 10.3 The CalculatorBean with Accessors package com.brainysoftware; public class CalculatorBean { private int memory; public void setMemory(int number) { memory = number; } public int getMemory() { return memory; } public int doubleIt(int number) { return 2 * number; } }

Using the jsp:setProperty and jsp:getProperty action elements, you can set and obtain the value of memory, as demonstrated in the JSP page in Listing 10.4.
Listing 10.4 Accessing the Property Using jsp:setProperty and jsp:getProperty <jsp:useBean id="theBean" class="com.brainysoftware.CalculatorBean"/> <jsp:setProperty name="theBean" property="memory" value="169"/> The value of memory is <jsp:getProperty name="theBean" property="memory"/>

Listing 10.5 Calling Accessor Methods Like Ordinary Methods <jsp:useBean id="theBean" class="com.brainysoftware.CalculatorBean"/> <% theBean.setMemory(987); %> The value of memory is <%= theBean.getMemory()%>

In many cases, it is also common to use an expression as the value of the value attribute. For example, the code in Listing 10.6 feeds a JSP expression to the value attribute in a jsp:setProperty action element.
Listing 10.6 Feeding a JSP Expression to the Value Attribute <jsp:useBean id="theBean" class="com.brainysoftware.CalculatorBean"/> <% int i = 2; %> <jsp:setProperty name="theBean" property="memory" value="<%= 100 * i %>"/> The value of memory is <jsp:getProperty name="theBean" property="memory"/>

Setting a Property Value from a Request

You can use a more concise way of setting a property value using the jsp:setProperty action element if the value happens to come from the Request object's parameter(s). In fact, there are three forms of syntax, as discussed in the previous section. The three forms are re-written here:
<jsp:setProperty name="Bean Name" property="PropertyName" param="parameterName"/> <jsp:setProperty name="Bean Name" property="PropertyName"/> <jsp:setProperty name="Bean Name" property="*"/>

The first form allows you to assign a Request object parameter value to a property. The param attribute of the jsp:setProperty action element must be assigned the name of the Request object's parameter name. The following example demonstrates the use of this form. The example has two pages. The first page is a simple HTML with a form containing a Text box into which the user can enter a number. The code for this page is shown in Listing 10.7.
Listing 10.7 The Page Where the User Can Type a Number <HTML> <HEAD> <TITLE>Passing a value</TITLE> </HEAD> <BODY> <CENTER> Please type in a number in the box <BR> <FORM METHOD=POST ACTION=SimplePage.jsp> <INPUT TYPE=TEXT NAME=memory> <INPUT TYPE=SUBMIT> </FORM> </CENTER> </BODY> </HTML>

Note that the TEXT element name is memory and the form is passed to a JSP page called SimplePage.jsp. The SimplePage.jsp is given in Listing 10.8.

Listing 10.8 The SimplePage.jsp Page <jsp:useBean id="theBean" class="com.brainysoftware.CalculatorBean"/> <jsp:setProperty name="theBean" property="memory" param="memory"/> The value of memory is <jsp:getProperty name="theBean" property="memory"/>

If the parameter name is the same as the bean's property name, you can even omit the param attribute:
<jsp:setProperty name="theBean" property="memory"/>

This is, of course, the third form of the jsp:setProperty syntax. The last form allows you to assign the values of Request object's parameters to multiple properties in a bean as long as the names of the properties are the same as the parameter names. The second line in Listing 10.8 can be replaced with the following:
<jsp:setProperty name="theBean" property="*"/>

Potrebbero piacerti anche