Java Reference
In-Depth Information
The keyword
id
identifies the name of the bean and the
class
keyword identifies the Java class to which the bean
refers. The tag essentially performs the same function as the following Java scriptlet:
<% c9java.Employee EmpBean = new c9java.Employee();%>
The key difference between the useBean tag and the scriptlet is the tag's scope parameter. The
scope
keyword
defines which other objects can access the bean. The four scope values that can be specified are
page
,
request
,
session
, and
application
.
Page
means that only this JSP can use the bean.
Request
ties the bean to the request that
was passed to the JSP. This means that any object that can access the request can access the bean. When
session
is
specified, any object created during the user's browser session can access the bean. Specifying
application
means
that any object within the project created by any session (i.e., any user) can access this bean. Of course, if the server is
rebooted, all beans are deleted regardless of scope.
To perform the same function of the scope parameter with Java commands we would define the new employee
object as an attribute of the session, request, etc. For example, the following statements would define the variable
EmpBean a session bean called EmpBean.
HttpSession session = req.getSession();
session.setAttribute("EmpBean", EmpBean);
A request object has a method called getSession, which, as the name implies returns the current browser session
as an object. Session objects can have attributes and we use the session object's setAttribute method to define
EmpBean as an attribute of the current session. This means EmpBean can be accessed by any other object created in
the session. In other words, the programmer does not have to pass variables to share objects.
Regardless of how the bean is defined (tags or scripts), bean property values are assigned and retrieved by
setProperty
and
getProperty
tags. These tags invoke an object's getters and setters. For instance, the following
setProperty tag:
<jsp:setProperty name="EmpBean" property="empName"
param="empNameTF" />
performs that same function as the following scriptlet:
<% EmpBean.setEmpName(request.getParameter("empNameTF")); %>
Notice how simple the setProperty tag syntax is compared to the Java statement. Once again, the server's
JSP container is making things easy for you. When converting the JSP to a servlet, the JSP container translates the
information specified in the tags into the complex Java statements. For example, because we used the keyword
param,
the container recognizes EmpNameTF as a request object parameter. (You can specify
value
instead of
param
to assign
a static value to the property.) As a matter of fact, if the bean property has the same name as the request parameter
(i.e., if we had called the text field empName instead of empNameTF) we could have coded the tag as follows:
<jsp:setProperty name="EmpBean" property="empName"/>
In addition, if the parameter value must be converted from one type to another type (e.g., from a primitive to string)
the container automatically inserts the code to perform the conversion.
To retrieve a parameter, use a getProperty tag as follows:
<jsp:getProperty name="EmpBean" property="empName"/>