How to pre-compile JSP?
Add jsp_precompile as a request parameter and send a request to the
JSP file. This will make the jsp pre-compile. Why it is mentioned as pre
compile instead of compilation is that, the request is not served. That
is, the JSP will not be executed and the request will not be serviced.
Just it will be compiled and the implementation class will be generated.
The jsp_precompile parameter may have no value, or may have values true or false. It should not have any other values like ?jsp_precompile=yes – this will result in HTTP error 500.
So the example for query strings to pre compile jsp files are
?jsp_precompile
?jsp_precompile=true
?jsp_precompile=false
?foobar=foobaz&jsp_precompile=true
?foobar=foobaz&jsp_precompile=false
But if you want to pre compile in server independent manner you have to use jsp_precompile request parameter and no other option available. To do it for the whole application, you can custome write a servlet or jsp file which will raise a request for all the JSPs available with jsp_precompile added as parameter.
I will give an example for the popular Apache Tomcat using ANT tool to pre compile JSP files. Important step is to write the ANT build script which will take through the compilation process. Using this ant script, either you can pre compile JSP files from command prompt or you can integrate the step of pre compiling JSP files into the build process which creates the distributable (WAR) file of your application.
Use the above ant script and execute the following command to pre compile JSP files from command prompt:
$ANT_HOME/bin/ant -Dtomcat.home=<$TOMCAT_HOME> -Dwebapp.path=<$WEBAPP_PATH>
Since the Java compiler is not needed, there will be a reduction of the footprint needed to run a JSP container.
In include directives, tag library references, and translation-time actions used in custom actions, compilation of a JSP page provides resolution of relative URL specifications.
The jsp_precompile parameter may have no value, or may have values true or false. It should not have any other values like ?jsp_precompile=yes – this will result in HTTP error 500.
So the example for query strings to pre compile jsp files are
?jsp_precompile
?jsp_precompile=true
?jsp_precompile=false
?foobar=foobaz&jsp_precompile=true
?foobar=foobaz&jsp_precompile=false
How to do pre compile for a bunch of JSP files? for all JSPs in a folder or application?
There are no special features available for this in the JSP specification. But the application servers (JSP containers) provide methods to do this on their own way. At the end of this tutorial we will see how to pre compile JSP files for Apache Tomcat 6.But if you want to pre compile in server independent manner you have to use jsp_precompile request parameter and no other option available. To do it for the whole application, you can custome write a servlet or jsp file which will raise a request for all the JSPs available with jsp_precompile added as parameter.
Following JSP will pre compile all JSPs available in a folder and its subfolder:
<%@ page import="javax.servlet.*,javax.servlet.http.*,javax.servlet.jsp.* "%> <%@ page import="java.util.Set,java.util.Iterator,java.io.IOException"%> <%! private void preCompileJsp(PageContext pageContext, JspWriter out, HttpServletRequest request, HttpServletResponse response, String uripath) throws IOException, ServletException { // getting jsp files list for pre compilation Set jspFilesSet = pageContext.getServletContext().getResourcePaths(uripath); for (Iterator jspFilesSetItr = jspFilesSet.iterator(); jspFilesSetItr.hasNext();) { String uri = (String) jspFilesSetItr.next(); if (uri.endsWith(".jsp")) { RequestDispatcher rd = getServletContext().getRequestDispatcher(uri); if (rd == null) throw new Error(uri +" - not found"); rd.include(request, response); } else if (uri.endsWith("/")) { preCompileJsp(pageContext, out, request, response, uri); } } } %> < html >< head >< title >Pre Compile JSP Files</ title ></ head > < body > <% HttpServletRequest req = new HttpServletRequestWrapper(request) { public String getQueryString() { // setting the pre compile parameter return "jsp_precompile"; }; }; preCompileJsp(pageContext, out, req, response, "/"); %> JSP files Pre Compile Completed. </ body ></ html > |
How to pre compile JSP from command line?
Yes you can compile JSP files from command line. There are no specific tool promoted or authored by JSP specification or SUN. The right choice is to depend on application server for which we are compiling application or JSP the JSP files. Logic is to fork the JSP compiler mostly JSPC from command prompt using a utility like ANT.I will give an example for the popular Apache Tomcat using ANT tool to pre compile JSP files. Important step is to write the ANT build script which will take through the compilation process. Using this ant script, either you can pre compile JSP files from command prompt or you can integrate the step of pre compiling JSP files into the build process which creates the distributable (WAR) file of your application.
Apache Tomcat 6.0 uses Eclipse JDT Java compiler to perform JSP java source code compilation.
< project name = "Webapp Precompilation" default = "all" basedir = "." > < import file = "${tomcat.home}/bin/catalina-tasks.xml" /> < target name = "jspc" > < jasper validateXml = "false" uriroot = "${webapp.path}" webXmlFragment = "${webapp.path}/WEB-INF/generated_web.xml" outputDir = "${webapp.path}/WEB-INF/src" /> </ target > < target name = "compile" > < mkdir dir = "${webapp.path}/WEB-INF/classes" /> < mkdir dir = "${webapp.path}/WEB-INF/lib" /> < javac destdir = "${webapp.path}/WEB-INF/classes" optimize = "off" debug = "on" failonerror = "false" srcdir = "${webapp.path}/WEB-INF/src" excludes = "**/*.smap" > < classpath > < pathelement location = "${webapp.path}/WEB-INF/classes" /> < fileset dir = "${webapp.path}/WEB-INF/lib" > < include name = "*.jar" /> </ fileset > < pathelement location = "${tomcat.home}/lib" /> < fileset dir = "${tomcat.home}/common/lib" > < include name = "*.jar" /> </ fileset > < fileset dir = "${tomcat.home}/bin" > < include name = "*.jar" /> </ fileset > </ classpath > < include name = "**" /> < exclude name = "tags/**" /> </ javac > </ target > < target name = "all" depends = "jspc,compile" > </ target > < target name = "cleanup" > < delete > < fileset dir = "${webapp.path}/WEB-INF/src" /> < fileset dir = "${webapp.path}/WEB-INF/classes/org/apache/jsp" /> </ delete > </ target > </ project > |
$ANT_HOME/bin/ant -Dtomcat.home=<$TOMCAT_HOME> -Dwebapp.path=<$WEBAPP_PATH>
The benefits of pre-compiling a JSP page:
It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.Since the Java compiler is not needed, there will be a reduction of the footprint needed to run a JSP container.
In include directives, tag library references, and translation-time actions used in custom actions, compilation of a JSP page provides resolution of relative URL specifications.
0 comments:
Post a Comment