Download JavaServer Pages (JSP): Bonus for Java Developers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1348
JavaServer Pages (JSP): Bonus for Java Developers
Chapter 37
37.6.3 <jsp:useBean> Action
Action <jsp:useBean> enables a JSP to manipulate a Java object. This action creates a
Java object or locates an existing object for use in the JSP. Figure 37.13 summarizes action
<jsp:useBean>’s attributes. If attributes class and beanName are not specified, the JSP
container attempts to locate an existing object of the type specified in attribute type. Like
JSP implicit objects, objects specified with action <jsp:useBean> have page, request,
session or application scope that indicates where they can be used in a Web application. Objects with page scope are accessible only by the page in which they are defined.
Multiple JSP pages potentially can access objects in other scopes. For example, all JSPs
that process a single request can access an object in request scope.
Common Programming Error 37.6
One or both of the <jsp:useBean> attributes class and type must be specified; otherwise, a translation-time error occurs.
37.6
Many Web sites place rotating advertisements on their Web pages. Each visit to one of
these pages typically results in a different advertisement being displayed in the user’s Web
browser. Typically, clicking an advertisement takes you to the Web site of the company
that placed the advertisement. Our first example of <jsp:useBean> demonstrates a
simple advertisement rotator bean that cycles through a list of five advertisements. In this
example, the advertisements are covers for some of our books. Clicking a cover takes you
to the Amazon.com Web site where you can read about and possibly order the book.
The Rotator bean (Fig. 37.14) has three methods: getImage, getLink and nextAd.
Method getImage returns the image file name for the book cover image. Method
getLink returns the hyperlink to the book at Amazon.com. Method nextAd updates the
Rotator so the next calls to getImage and getLink return information for a different
advertisement. Methods getImage and getLink each represent a read-only JavaBean
property—image and link, respectively. Rotator keeps track of the current advertisement with its selectedIndex variable, which is updated by invoking method nextAd.
Attribute
Description
id
The name used to manipulate the Java object with actions <jsp:setProperty> and <jsp:getProperty>. A variable of this name is also declared for
use in JSP scripting elements. The name specified here is case sensitive.
scope
The scope in which the Java object is accessible—page, request, session or
application. The default scope is page.
class
The fully qualified class name of the Java object.
beanName
The name of a bean that can be used with method instantiate of class
java.beans.Beans to load a JavaBean into memory.
type
The type of the JavaBean. This can be the same type as the class attribute, a
superclass of that type or an interface implemented by that type. The default
value is the same as for attribute class. A ClassCastException occurs if
the Java object is not of the type specified with attribute type.
Fig. 37.13 Attributes of the <jsp:useBean> action.
©1981–2004, Deitel & Associates, Inc. All rights reserved.