Recipe 5.3 Precompiling JSPs with the Precompilation Protocol


Problem

You want to use the "precompilation protocol" that is part of the JSP specification to precompile one or more JSP files.

Solution

Send a request to the JSP container that includes a jsp_precompile parameter.

Discussion

The JSP 1.2 and 2.0 specifications require compliant JSP containers to support the use of the jsp_precompile request parameter. This parameter suggests that the container precompile the requested JSP. Here is how it works in Tomcat:

  1. Request the JSP that you want precompiled with the jsp_precompile parameter added to the URL, as in http://localhost:8080/home/url_rewrite.jsp?jsp_precompile=true .

    The JSP container is not supposed to execute the JSP page; it just precompiles it. The result of the request, if you were making it in a web browser, is a blank page.

  2. If the JSP file in JSP page syntax has not yet been compiled, or if the JSP file has been changed and has a later modification date than any existing page implementation class, Tomcat creates a new Java source and class file for the JSP in the <Tomcat-install-directory>/work directory. If the JSP file is named url_rewrite.jsp , Tomcat calls the Java source and class files url_rewrite_jsp.java and url_rewrite_jsp.class .

    Supplying the request parameter jsp_precompile (without the "=true" part) is the same as requesting jsp_precompile =true in the URL.

The precompilation protocol in Tomcat will both create the .java file and compile that file into the JSP page implementation class. Using the JspC tool as described in Recipe 5.1 will generate only a .java file.


This protocol is best used with an automated tool that can make HTTP requests , such as the Jakarta Commons HttpClient component. Using such a tool allows you to automate the precompilation of dozens of JSPs by sending several HTTP requests from a single Java program.

See Also

Recipe 5.1 on using Tomcat's JspC utility; Recipe 5.2 on precompiling with WebLogic Server; Recipe 5.4 on mapping the compiled JSPs in web.xml ; Chapter 7 on sending HTTP requests from a servlet or a JSP; the Jakarta Commons HttpClient homepage at http://jakarta.apache.org/commons/httpclient/; The JSP precompilation section of JavaServer Pages by Hans Bergsten (O'Reilly); Chapter JSP.11.4 of the JSP 2.0 specification.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

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