JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Using JavaBean Classes

Part:   1  2  3  4  5 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

It's very interesting to see the Servlet class translated from UseBean.jsp:

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
import CacheBean;

public class UseBean_jsp extends HttpJspBase {

  private static java.util.Vector _jspx_includes;

  public java.util.List getIncludes() {
    return _jspx_includes;
  }

  public void _jspService(HttpServletRequest request, 
        HttpServletResponse response)
        throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
    javax.servlet.jsp.PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;

    try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/xml;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("<html>");
      out.write("<body>");
      CacheBean b = null;
      synchronized (pageContext) {
        b = (CacheBean) pageContext.getAttribute("b", 
             PageContext.PAGE_SCOPE);
        if (b == null){
          try {
            b = (CacheBean) java.beans.Beans.instantiate(
                this.getClass().getClassLoader(), "CacheBean");
          } catch (ClassNotFoundException exc) {
            throw new InstantiationException(exc.getMessage());
          } catch (Exception exc) {
            throw new ServletException("Cannot create bean of class " 
               + "CacheBean", exc);
          }
          pageContext.setAttribute("b", b, PageContext.PAGE_SCOPE);
        }
      }
      JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("b"),
         "text", "Hello world!",null, null, false);
      out.write("\nProperty from my Bean: ");
      out.print(JspRuntimeLibrary.toString(((
         (CacheBean)pageContext.findAttribute("b")).getText())));
      out.write("<br/>");
      out.write("\nInfo from my Bean: ");
      out.print(b.getInfo());
      out.write("</body>");
      out.write("</html>");
    } catch (Throwable t) {
      out = _jspx_out;
      if (out != null && out.getBufferSize() != 0)
        out.clearBuffer();
      if (pageContext != null) pageContext.handlePageException(t);
    } finally {
      if (_jspxFactory != null) 
          _jspxFactory.releasePageContext(pageContext);
    }
  }
}

Note that:

  • The "page" directive element with "import" attribute was translated into a true "import" Java statement.
  • The "useBean" action element was translated into a block of code to load bean class, instantiate an object of that bean class, and put that object into the attribute collection in the page context object. The name of the attribut is set to the same as the object name.
  • The "setProperty" action element was translated into a call to introspecthelper() method.
  • The "getProperty" action element was translated into a call to a "get" method.
  • The "expression" element was translated into a call to "out.print()".

Compilation Errors with Tomcat 4.1.18 and JDK 1.4.1

If you are using JDK 1.4.1 and trying to run the above example, you will get the following compilation error:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 8 in the jsp file: /UseBean.jsp

Generated servlet error:
    [javac] Compiling 1 source file

C:\local\jakarta-tomcat-4.1.18\work\Standalone\localhost
   \_\UseBean_jsp.java:7: '.' expected
import CacheBean;

This is because JDK 1.4.1 does not allow import statement to be used on classes in unnamed packages any more. For more detail information, please see section "Tomcat 4.1.18 with JDK 1.4.1" in this book.

Setting and Getting JavaBeans Properties

As you can see from previous sections, a JavaBean is just a regular Java obect. Once a JavaBean is created in a JSP, you can use JSP setProperty and getProperty action elements to set and retrieve values of its properties. To support these action elements, a JavaBean class must implement set and get methods based on some rules.

(Continued on next part...)

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Using JavaBean Classes