Chapter 14 Using Java Beans Components in JSP
Chapter 14 Using Java. Beans Components in JSP Documents
Contents 1. 2. 3. 4. 5. 6. Why use Beans? What are Beans? Using Beans: Basic tasks Setting Bean properties: Advanced Techniques Sharing Beans Practice exercises
1. Why Use Beans? n n n Separate the classes are easier to write, compile, test, debug and reuse. Beans are merely regular Java classes that follow some simple conventions defined by the Java. Beans specification. Beans extend no particular class Beans are in no particular package, Beans use no particular interface.
1. Why Use Beans? (cont. ) n n n Visual manipulation tools and other programs can automatically discover information about classes that follow this format Can create and manipulate the classes without the user having to explicitly write any code. In JSP in particular, use of Java. Beans components provides three advantages over scriptlets and JSP expressions that refer to normal Java classes.
1. Why use Beans? (Cont. ) No Java Syntax n By using beans, can manipulate Java objects using only XML-compatible syntax: n n no parentheses, semicolons, or curly braces. Simpler object sharing. When you use the JSP bean constructs, you can much more easily share objects among multiple pages or between requests than if you use the equivalent explicit Java code.
2. What Are Beans? n n n A bean class must have a zero-argument (default) constructor. A bean class should have no public instance variables (fields). Persistent values should be accessed through methods called get. Xxx and set. Xxx.
3. Using Beans: Basic Tasks n Using three main constructs to build and manipulate Java. Beans components in JSP pages. n n n jsp: use. Bean. In the simplest case, this element builds a new bean. It is normally used as follows: <jsp: use. Bean id="bean. Name" class="package. Class" /> If you supply a scope attribute , the jsp: use. Bean element can either build a new bean or access a preexisting one.
jsp: get. Property n n This element reads and outputs the value of a bean property. Reading a property is a shorthand notation for calling a method of the form get. Xxx. n <jsp: get. Property name="bean. Name" property="property. Name" />
jsp: set. Property n This element modifies a bean property n <jsp: set. Property name="bean. Name" property="property. Name" value="property. Value" />.
Building Beans: jsp: use. Bean n n The jsp: use. Bean action lets you load a bean to be used in the JSP page. The simplest syntax: n <jsp: use. Bean id="name" class="package. Class" />
Example n n The JSP action: <jsp: use. Bean id="book 1" class="coreservlets. Book" /> As equivalent to the scriptlet: <% coreservlets. Book book 1 = new coreservlets. Book(); %>
4. Setting Bean Properties: Advanced Techniques n Using jsp: set. Property to set bean properties takes three attributes: n name (which should match the id given by jsp: use. Bean) property (the name of the property to change) value (the new value).
4. Setting Bean Properties: Advanced Techniques n For example, the Sale. Entry class shown in Listing 14. 3 has: n n n item. ID property (a String), num. Items property (an int), discount. Code property (a double), and two read-only properties, item. Cost and total. Cost (each of type double). Listing 14. 4 shows a JSP file that builds an instance of the Sale. Entry class by means of:
4. Setting Bean Properties: Advanced Techniques n n n <jsp: use. Bean id="entry" class="coreservlets. Sale. Entry" /> Listing 14. 5 gives the HTML form that collects the request parameters. The results are shown in Figure 14 -4.
4. Setting Bean Properties: Advanced Techniques Once the bean is instantiated, using a request parameter to set the item. ID is straightforward, as shown below. n <jsp: set. Property name="entry" property="item. ID" value='<%= request. get. Parameter("item. ID") %>' /> n
4. Setting Bean Properties: Advanced Techniques n n Most JSP attribute values have to be fixed strings, but the value attribute of jsp: set. Property is permitted to be a request time expression. If the expression uses double quotes internally, In any case, the point is that it is possible to use JSP expressions here, but doing so requires the use of explicit Java code. The next two subsections will discuss how to solve these problems.
Associating Individual Properties with Input Parameters n n n associate a property with a request parameter and automatically perform type conversion from strings to numbers, characters, and boolean values you use param to name an input parameter <jsp: set. Property name="entry" property="num. Items" param="num. Items" />
Associating Individual Properties with Input Parameters (Cont. ) n n n You can simplify the code slightly if the request parameter name and the bean property name are the same. In that case, you can omit param as in the following example. <jsp: set. Property name="entry" property="num. Items" /> <%-- param="num. Items" is assumed. --%>
Associating All Properties with Request Parameters <jsp: set. Property name="entry" property="*" /> <jsp: use. Bean id="entry" class="coreservlets. Sale. Entry" /> <jsp: set. Property name="entry" property="*" />
Associating All Properties with Request Parameters (Cont. ) Note: § No action is taken when an input parameter is missing § Automatic type conversion does not guard against illegal values as effectively as does manual type conversion § Bean property names and request parameters are case sensitive
5. Sharing Beans n n Up to this point, the objects that were created with jsp: use. Bean as though they were simply bound to local variables in the _jsp. Service method. That is not the only behavior. They are also stored in one of four different locations, depending on the value of the optional scope attribute of jsp: use. Bean.
5. Sharing Beans n n n When you use scope, the system first looks for an existing bean of the specified name in the designated location. Only when the system fails to find a preexisting bean does it create a new one. The scope attribute has four possible values: page (the default), request, session, and application.
<jsp: use. Bean. . . scope="page" /> n Every page and every request has a different Page. Context object, using scope="page" (or omitting scope) indicates that the bean is not shared and thus a new bean will be created for each request
<jsp: use. Bean. . . scope="request" /> n This value signifies that, in addition to being bound to a local variable, the bean object should be placed in the Http. Servlet. Request object for the duration of the current request, where it is available by means of the get. Attribute method
<jsp: use. Bean. . . scope="session“ /> n This value means that, in addition to being bound to a local variable, the bean will be stored in the Http. Session object associated with the current request, where it can be retrieved with get. Attribute.
<jsp: use. Bean. . . scope="application" /> n This value means that, in addition to being bound to a local variable, the bean will be stored in the Servlet. Context available through the predefined application variable or by a call to get. Servlet. Context. The Servlet. Context is shared by all servlets and JSP pages in the Web application. Values in the Servlet. Context can be retrieved with the get. Attribute method.
6. Practice Exercise 1 n n Using Beans to develop a web application that allows student to calculate the average score. Template interface:
- Slides: 27