Session Management in JSP
 
Using JavaBean Components
 
The component model for JSP technology is based on JavaBeans component architecture. JavaBeans components are nothing but Java objects which follow a well-defined design/naming pattern: the bean encapsulates its properties by declaring them private and provides public accessor (getter/setter) methods for reading and modifying their values.
 
Before we can access a bean within a JSP page, it is necessary to identify the bean and obtain a reference to it.
 
The <jsp:useBean> tag tries to obtain a reference to an existing instance using the specified id and scope, as the bean may have been previously created and placed into the session or application scope from within a different JSP page.
 
The bean is newly instantiated using the Java class name specified through the class attribute only if a reference was not obtained from the specified scope.
 
 
Consider the tag:
<jsp:useBean id="user" class="eBIZ.com.Person"
scope="session" />
In this example, the Person instance is created just once and placed into the session.
 
If this useBean tag is later encountered within a different JSP page, a reference to the original instance that was created before is retrieved from the session.
 
The <jsp:useBean> tag can also optionally include a body, such as :
<jsp:useBean id="user" class="eBIZ.com.Person"
scope="session">
<%
user.setDate(DateFormat.getDateInstance( ).format(new Date()));
//etc..
%>
</jsp:useBean>
 
Any scriptlet (or <jsp:setProperty> tags) present within the body of a <jsp:useBean> tag are executed only when the bean is instantiated, and are used to initialize the bean's properties.
 
Once we have declared a JavaBean component, we have access to its properties to customize it.
 
The value of a bean's property is accessed using the <jsp:getProperty> tag. With the <jsp:getProperty> tag, we specify the name of the bean to use (from the id field of useBean), as well as the name of the property whose value we are interested in. The actual value is then directly printed to the output:
<jsp:getProperty name="user" property="name" />
 
Changing the property of a JavaBean component requires you to use the <jsp:setProperty> tag.
 
For this tag, we identify the bean and property to modify and provide the new value:
<jsp:setProperty name="user" property="name"
value="eBIZ.com" />
or
<jsp:setProperty name="user" property="name"
value="<%=expression %>" />
 
When developing beans for processing form data, we can follow a common design pattern by matching the names of the bean properties with the names of the form input elements.
 
We also need to define the corresponding getter/setter methods for each property within the bean. The advantage in this is that we can now direct the JSP engine to parse all the incoming values from the HTML form elements that are part of the request object, then assign them to their corresponding bean properties with a single statement, like this:
<jsp:setProperty name="user" property="*"/>
 
This runtime magic is possible through a process called introspection, which lets a class expose its properties on request.
 
The introspection is managed by the JSP engine, and implemented through the Java reflection mechanism. This feature alone can be a lifesaver when processing complex forms containing a significant number of input elements.
 
If the names of our bean properties do not match those of the form's input elements, they can still be mapped explicitly to our property by naming the parameter as:
<jsp:setProperty name="user" property="address"
param="parameterName" />