<jsp:useBean id="connection" type="com.myco.myapp.myBean"
/> - Bean should already exist
<jsp:useBean id="name" scope="page|request|session|application"
typeSpec />
typeSpec class="className" |
class="className" type="typeName" |
type="typeName" class="className" |
beanName="beanName" type="typeName" |
type="typeName" beanName="beanName" |
type="typeName"
- First the container looks for the specified bean
in the specified scope (default scope is page), if it
is not there, one will be created. With beanName, you
can specify a serialized file for instantiating the
bean from.
- class or beanName is required, type is optional.
- If class and beanName are not specified (only type
is specified), the bean will not be created, it should
already exist in the specified scope.
- The specified class should not be an abstract class
or an interface and should have a public no-args constructor.
If type is specified, the instantiated class is casted
to the type. Instantiate method is used to create the
bean.
- jsp:useBean action element can have a non-empty
body, e.g. a jsp:setProperty action; the body will be
executed for newly instantiated beans only
<jsp:useBean id="name" scope="page|request|session|application"
typeSpec >
body - usually this will be scriptlets or jsp:setProperty
actions
</jsp:useBean>
<jsp:getProperty>
<jsp:getProperty name="beanName" property="propName"
/>
<jsp:setProperty>
<jsp:setProperty name="bean1" property="*" />
-- all request parameters will be set in bean's properties
(with matching names)
<jsp:setProperty name="bean2" property="user " param="username
" />
-- request parameter 'username' will be set to 'user'
property of the bean
<jsp:setProperty name="bean2" property="user " />
-- request parameter 'user' will be set to 'user' property
of the bean (since no param specified, it is assumed to
have the same name as the property)
<jsp:setProperty name="bean2" property="user "
value="me" />
<jsp:setProperty name="bean2" property="user " value="<%=session.getAttribute("user")
%>" />
-- new value can be specified with an expression. If
it's an expression no conversion is performed, else standard
conversion is performed.
Note: value cannot be used in conjunction with
param
10.2 Given JSP page attribute scopes: request, session,
application, identify the equivalent servlet code.
request - HTTPServletRequest
session - HTTPServletRequest.getSession() : HTTPSession
application - GenericServlet.getServletContext() or GenericServlet.getServletConfig().getServletContext()
10.3 Identify techniques that access a declared
JavaBean component.
A declared JavaBean can also be accessed using: (the
name specified in the id attribute of <jsp:useBean>)
Scriptlets
Expressions
Custom Tags
Previous Contents Next