Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Process JSPs effectively with JavaBeans

Transport JSP processing logic into a JavaBean with the Template Method design pattern

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 7

Now that I've covered the top-level view, I'll switch to bottom-up mode by examining one JSP page.

JSP example

Each page must adhere to specific structure in order to comply with the framework.

Listing 1. login.jsp

<%@ page import = "lbm.jsputil.*" %>
<jsp:useBean id="_loginJSPBean" class="lbm.examples.LoginJSPBean"
scope="session"/>
<jsp:setProperty name="_loginJSPBean" property="*"/>
<% AbstractJSPBean _abstractJSPBean = _loginJSPBean; %>
<%@ include file="includeheader.jsp" %>
<html>
<head><title>Vote Login</title></head>
<body bgcolor="white">
<font size=4>
  Please enter your Voter ID and Password
</font>
<font size="3" color="Red">
  <jsp:getProperty name="_loginJSPBean" property="errorMsg"/>
</font>
<font size=3>
  <form method=post>
    Voter ID <input type=text name=voterId value=<jsp:getProperty
       name="_loginJSPBean" property="voterId"/>>
Password <input type=password name=password value=<jsp:getProperty name="_loginJSPBean" property="password"/>>

<input type=submit value="Login"> </form> </font> </body> </html> <%@ include file="includefooter.jsp" %>


The JSP's structure is as follows: The page begins with several JSP statements. The HTML code that follows is free from JSP directives, statements, scriptlets, and so forth. The only exceptions are <jsp:getProperty> directives. You should typically use those directives to retrieve dynamic content from the bean. Finally, the page ends with one JSP include directive.

Let's examine some of those essential JSP statements.

<jsp:useBean id="_loginJSPBean" class="lbm.examples.LoginJSPBean" scope="session"/>
<jsp:setProperty name="_loginJSPBean" property="*"/>


The above code establishes a link between the JSP and the corresponding bean. The second statement implicitly passes values of all form fields (stored as HTTP request parameters) to the matching properties in the bean. The code uses the bean's setter methods. For more information about how that works, check Govind Seshadri's "Advanced form processing using JSP".

<% AbstractJSPBean _abstractJSPBean = _loginJSPBean; %>
<%@ include file="includeheader.jsp" %>


The first statement above will enable the includeheader.jsp to perform the common processing. includeheader.jsp is statically included in the second statement. Note that _loginJSPBean and _abstractJSPBean now refer to the same object, just with different interfaces.

Listing 2. includeheader.jsp

<%-- Set the SharedSessionBean --%>
<jsp:useBean id="_sharedSessionBean" class="lbm.jsputil.SharedSessionBean" scope="session"/>
<% _abstractJSPBean.setSharedSessionBean(_sharedSessionBean); %>
<%-- Set implicit Servlet objects --%>
<% _abstractJSPBean.setRequest(request); %>
<% _abstractJSPBean.setResponse(response); %>
<% _abstractJSPBean.setServlet(this); %>
<%-- Perform the processing associated with the JSP --%>
<% _abstractJSPBean.process(); %>
<%-- If getSkipPageOutput equals false, do not output the JSP page --%>
<% if (! _abstractJSPBean.getSkipPageOutput()) { %>


includeheader.jsp is one of the pattern's core elements. All JSPs use this common element.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources