Home
TOC Index |
Creating and Using a JavaBeans Component
You declare that your JSP page will use a JavaBeans component using either one of the following formats:
<jsp:useBean id="beanName
" class="fully_qualified_classname
" scope="scope
"/><jsp:useBean id="beanName
" class="fully_qualified_classname
" scope="scope
"> <jsp:setProperty .../> </jsp:useBean>The second format is used when you want to include
jsp:setProperty
statements, described in the next section, for initializing bean properties.The
jsp:useBean
element declares that the page will use a bean that is stored within and accessible from the specified scope, which can beapplication
,session
,request
, orpage
. If no such bean exists, the statement creates the bean and stores it as an attribute of the scope object (see Using Scope Objects). The value of theid
attribute determines the name of the bean in the scope and the identifier used to reference the bean in other JSP elements and scriptlets.
Note: In JSP Scripting Elements, we mentioned that you must import any classes and packages used by a JSP page. This rule is slightly altered if the class is only referenced byuseBean
elements. In these cases, you must only import the class if the class is in the unnamed package. For example, in What Is a JSP Page?, the pageindex.jsp
imports theMyLocales
class. However, in the Duke's Bookstore example, all classes are contained in packages and thus are not explicitly imported.
The following element creates an instance of
Currency
if none exists, stores it as an attribute of thesession
object, and makes the bean available throughout the session by the identifiercurrency
:<jsp:useBean id="currency" class="util.Currency" scope="session"/>
Home
TOC Index |
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.