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
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.
<%@ 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>
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.
<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>
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>
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.
Comments are closed for "How to pre-compile JSP?".
hey this is a very nice site for java interview questions, i have a similar site,but it’s in chinese, you’re welcome to visit and contact me.
Friend your tutorial is good
I have one question i want to hide all the jsp source codes so that when i made it as an application it doesnot show it to anyone how can i do that help me Thanks
not able to compile and run jsp programs
Thanks for the post. If possible, could you suggest a way to modify the precompile jsp so that statically included fragments don’t cause the compile to (possibly) fail?
In my case, static inclusion of fragments that are not “self-contained,” for example, when the included fragment references a variable that is declared in the parent jsp, (messy, I know), then “[variableName] cannot be resolved” compile errors result.
You are really awesome
please send me a mail with jsp page and how to execute that jsp page on commend prompt
Really awesome code.
UI is also pretty good
Another way to force precompiled JSP, for Weblogic AS
is including the following in weblogic.xml file
true
true true true
Another way to force precompiled JSP, for Weblogic AS
is via the jsp-descriptor in weblogic.xml file
Example:
true
-1
-1
-1
true
true
true
This is very good site for java learning stage and experienced guys bcoz the way of explaining is very nice.Thank you
great work man…
its too helpful…
thanks for sharing..
Thanks a lot for sharing. Your JSP compiler JSP saved my day.
Thanks.
I found it very useful. I was searching for some Eclipse plugins to do the work. But this definitely is useful for me. I just need to open one JSP to pre-compile all the JSp files.
Thanks once again.
Da, it would be nice if u start blogging about frameworks like struts2,EJB3 etc. I assure you full support…oakay?
Is there any way to reverse a compiled JSP back to the original JSP?
Good Site for study and interview.
too.. good..article.we also pre-compile JSPs in our business applications for faster performance.
Using above code (Pre compile JSP) if there is any compilation issue, calls doesn’t get forwarded so at a time u can get only one JSP which is failing.
Do u have any idea how to handle this , as i want complete report of a particular folder.
i have a doubt, in jsp page a button is there, when i click that button, one(Swing) java file can compile and run it, how?
I am getting below error.
…java:87: error: cannot find symbol Vector keygroup = new Vector();
Please suggest how to resolve it.
good post dear
any one can easily learn through this
thanks
Hi,
I have a situation. How to restrict the container for not to create the implicit objects for the JSP. Whenever required user only have to create.
Please let me know how can we do this.
Thanks,
Shanthi
great work … Thank u !!!
Following JSP will pre compile all JSPs available in a folder and its subfolder:
Thank you for giving such wonderful knowledge
Question : What is the uripath where we can get these path?
Please let me know in depth
I am having set of jsps ..i need the jsps to be pre-compiled.The jsp provided by you gives me idea but i dnt know how to use it.Where should it be placed and what is the filename that we need to keep for jsp
This solution works great with JBoss and Tomcat. Weblogic takes things a step further and runs the compiled jsp servlet.
Is there a way to force Weblogic only to generate + compile the servlet source but not run it like JBoss/Tomcat?