JSP Fundamentals

 < Free Open Study > 

JSP pages are converted to servlets by the JSP container in a process that is transparent to the developer. We'll author a simple JSP page that prints out the current date to demonstrate how this works:

     <html>       <head>         <title>Date</title>       </head>       <body>         <h3>           The date is           <% out.println((new java.util.Date()).toString()); %>         </h3>       </body>     </html> 

Notice how easy it is to combine HTML and Java code in a JSP page. It would be a lot easier to alter the way the page is presented than it would be if the same page were written as a servlet. Save this JSP page as %CATALINA_HOME%/webapps/jsp/date.jsp, start Tomcat, and navigate to http://localhost:8080/date.jsp:

click to expand

Quite a lot of things happen behind the scenes when a JSP page is deployed in a web container and is first served to a client request. Deploying and serving a JSP page involves two distinct phases:

  • Translation phase - In this phase the JSP page is transformed into a Java servlet and then compiled. This phase occurs only once for each JSP page and must be executed before the JSP page is served. The generated servlet class is known as a JSP page implementation class. The translation phase results in a delay when a JSP page is requested for the first time. To avoid this delay, JSP pages can be precompiled before they are deployed using tools that perform the translation phase when the server starts up.

  • Execution phase - This phase (also known as the request processing phase), is executed each time the JSP page is served by the web container. Requests for the JSP page result in the execution of the JSP page implementation class.

The actual form of the source code for the JSP page implementation class depends on the web container. Normally, the class implements javax.servlet.jsp.HttpJspPage or javax.servlet.jsp.JspPage, both of which extend javax.servlet.Servlet. The following code (tidied up a bit) is the source for the page implementation class of date.jsp as generated by Tomcat 4, which stores these files in %CATALINA_HOME%/webapps/work.

The class extends HttpJspBase, which is a class provided by Tomcat that implements the HttpJspPage interface.

     package org.apache.jsp;     import javax.servlet.*;     import javax.servlet.http.*;     import javax.servlet.jsp.*;     import org.apache.jasper.runtime.*;     public class date$jsp extends HttpJspBase {     public class MyFirstJSP$jsp extends HttpJspBase {       static {}       public date$jsp() {}       private static boolean _jspx_inited = false; 

When a request comes for the JSP page, the container creates an instance of the page and calls the jspInit() method on the page implementation object. It's worth noting that it is up to the container's implementation whether a new instance of the page is used each time, or if a single instance is serviced by multiple threads (which is most common scenario):

       public final void _jspx_init()         throws org.apache.jasper.runtime.JspException {} 

The generated class overrides the _JSP pageservice() method. This method is called each time the JSP is accessed by a client browser. All the Java code and template text we have in our JSP pages normally go into this method. It is executed each time the JSP page is served by the web container:

       public void _JSP pageservice(HttpServletRequest request,                               HttpServletResponse response)           throws java.io.IOException, ServletException {         JspFactory _jspxFactory = null;         PageContext pageContext = null;         HttpSession session = null;         ServletContext application = null;         ServletConfig config = null;         JspWriter out = null;         Object page = this;         String _value = null;         try {           if (_jspx_inited == false) {             synchronized (this) {               if (_jspx_inited == false) {                   _jspx_init();                   _jspx_inited = true;             }         }       }       _jspxFactory = JspFactory.getDefaultFactory();       response.setContentType("text/html;charset=ISO-8859-1");       pageContext = _jspxFactory.getPageContext(this, request, response,                                                 "", true, 8192, true); 

Here we initialize the implicit variables (which we cover shortly):

          application = pageContext.getServletContext();          config = pageContext.getServletConfig();          session = pageContext.getSession();          out = pageContext.getOut(); 

Write the template text stored in the text file to the output stream:

           // HTML // begin [file="/date.jsp";from=(0,0);to=(7,6)]           out.write("<html>\r\n<head>\r\n<title>Date</title>\r\n" +                     "</head>\r\n<body>\r\n<h3>\r\n" +                     "The date is\r\n");           // end           // begin [file="/date.jsp";from=(7,8);to=(7,57)]           out.println((new java.util.Date()).toString());           // end           // HTML // begin [file="/date.jsp";from=(7,59);to=(10,7)]           out.write("\r\n    </h3>\r\n </body>\r\n</html>");           // end           } catch (Throwable t) {             if (out != null && out.getBufferSize() != 0) {               out.clearBuffer();             }             if (pageContext != null) {               pageContext.handlePageException(t);             }           } finally {              if (_jspxFactory != null) {                _jspxFactory.releasePageContext(pageContext);              }           }         }      } 

Now that we've covered the basic objectives and working principles of JSP pages, we'll take a closer look at the various features of JSP that enable the building of powerful enterprise-class presentation components.

 < Free Open Study > 

Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net