18.2 Behind the Scenes

Java Servlet Programming, 2nd Edition > 18. JavaServer Pages > 18.2 Behind the Scenes

 
< BACKCONTINUE >

18.2 Behind the Scenes

How does JSP work? Behind the scenes, the server automatically creates, compiles, loads, and runs a special servlet to generate the page's content, as shown in Figure 18-2. You can think of this special servlet as a background, workhorse servlet. The static portions of the HTML page are generated by the workhorse servlet using the equivalent of out.println( ) calls, while the dynamic portions are included directly. For example, the servlet shown in Example 18-2 might be the background workhorse for hello1.jsp running under Tomcat.[2]

[2] If you're interested in seeing the true servlet source code for a JSP page, it's generally placed somewhere under the temporary directory specified in the context attribute javax.servlet.context.tempdir (see Chapter 4). When you find the true servlet source, you're likely to see that it is more complicated and convoluted than what is shown here.

Figure 18-2. Generating JavaServer Pages
Example 18-2. The Autogenerated Workhorse Servlet for hello1.jsp
import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.beans.*; import java.io.*; import java.util.*; import org.apache.jasper.runtime.*; import org.apache.jasper.*; public class _0002fhello_00031_0002ejsphello1_jsp_0 extends HttpJspBase {     private static boolean _jspx_inited = false;     public _0002fhello_00031_0002ejsphello1_jsp_0() { }     public final void _jspx_init() throws JasperException { }     public void _jspService(HttpServletRequest request,                              HttpServletResponse response)                                 throws 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) {                 _jspx_init();                 _jspx_inited = true;             }             _jspxFactory = JspFactory.getDefaultFactory();             response.setContentType("text/html;charset=8859_1");             pageContext = _jspxFactory.getPageContext(this, request, response,                         "", true, 8192, true);             application = pageContext.getServletContext();             config = pageContext.getServletConfig();             session = pageContext.getSession();             out = pageContext.getOut();             // HTML // begin [file="C:\\hello1.jsp";from=(0,0);to=(4,0)]             out.write("<HTML>\r\n<HEAD><TITLE>Hello</TITLE></HEAD>\r\n<BODY>");             out.write("\r\n<H1>\r\n");             // end             // begin [file="C:\\hello1.jsp";from=(4,2);to=(11,0)]             if (request.getParameter("name") == null) {                out.println("Hello World");             }             else {               out.println("Hello, " + request.getParameter("name"));             }             // end             // HTML // begin [file="C:\\hello1.jsp";from=(11,2);to=(15,0)]             out.write("\r\n</H1>\r\n</BODY></HTML>\r\n\r\n");             // end         } catch (Exception ex) {             if (out.getBufferSize() != 0)                 out.clearBuffer();             pageContext.handlePageException(ex);         } finally {             out.flush();             _jspxFactory.releasePageContext(pageContext);         }     } }

The first time you access a JSP page, you may notice that it takes a short time to respond. This is the time necessary for the server to create and compile the background servlet, what has been called the first-person penalty. Subsequent requests should be as fast as ever because the server can reuse the servlet in memory. The one exception is when the .jsp file changes, in which case the server notices during the next request for that page and recompiles a new background servlet. If there's ever an error in compiling, you can expect the server to somehow report the problem, usually in the page returned to the client and/or a server log file.

You can avoid the first-person penalty by precompiling your JSPs. This can be done manually by making a request to each JSP page passing a query string of ?jsp_precompile="true". The request causes the server to compile the JSPs, but the special query string tells the server not to bother passing the request through to the JSP for handling. Servlet container vendors can make this process more automated and easier.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

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