How to pre-compile JSP?

Last modified on October 15th, 2014 by Joe.

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


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,"%>
  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);
         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>
  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.

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">
             outputDir="${webapp.path}/WEB-INF/src" />
  <target name="compile">
    <mkdir dir="${webapp.path}/WEB-INF/classes"/>
    <mkdir dir="${webapp.path}/WEB-INF/lib"/>
    <javac destdir="${webapp.path}/WEB-INF/classes"
           debug="on" failonerror="false"
        <pathelement location="${webapp.path}/WEB-INF/classes"/>
        <fileset dir="${webapp.path}/WEB-INF/lib">
          <include name="*.jar"/>
        <pathelement location="${tomcat.home}/lib"/>
        <fileset dir="${tomcat.home}/common/lib">
          <include name="*.jar"/>
        <fileset dir="${tomcat.home}/bin">
          <include name="*.jar"/>
      <include name="**" />
      <exclude name="tags/**" />
  <target name="all" depends="jspc,compile">
  <target name="cleanup">
        <fileset dir="${webapp.path}/WEB-INF/src"/>
        <fileset dir="${webapp.path}/WEB-INF/classes/org/apache/jsp"/>

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>

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.

Comments on "How to pre-compile JSP?"

  1. Jim says:

    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.

  2. Jamal says:

    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

  3. vmp says:

    not able to compile and run jsp programs

  4. plankscale says:

    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.

  5. Prashant Pandey says:

    You are really awesome

  6. pokuri says:

    please send me a mail with jsp page and how to execute that jsp page on commend prompt

  7. ajay says:

    Really awesome code.

    UI is also pretty good

  8. Jose Manuel says:

    Another way to force precompiled JSP, for Weblogic AS
    is including the following in weblogic.xml file

    true true true

  9. Jose Manuel says:

    Another way to force precompiled JSP, for Weblogic AS
    is via the jsp-descriptor in weblogic.xml file




  10. sreekanth says:

    This is very good site for java learning stage and experienced guys bcoz the way of explaining is very nice.Thank you

  11. praveen rohal says:

    great work man…
    its too helpful…
    thanks for sharing..

  12. Anirban says:

    Thanks a lot for sharing. Your JSP compiler JSP saved my day.

  13. Kiran says:

    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.

  14. Shereef says:

    Da, it would be nice if u start blogging about frameworks like struts2,EJB3 etc. I assure you full support…oakay?

  15. Dave says:

    Is there any way to reverse a compiled JSP back to the original JSP?

  16. Ram says:

    Good Site for study and interview.

  17. Anonymous says:

    too.. good..article.we also pre-compile JSPs in our business applications for faster performance.

  18. Arjun says:

    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.

  19. keerthi says:

    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?

  20. Dhaval says:

    I am getting below error.
    …java:87: error: cannot find symbol Vector keygroup = new Vector();
    Please suggest how to resolve it.

  21. mintu singh says:

    good post dear
    any one can easily learn through this

  22. Shanthi says:


    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.


  23. Chathuranga Prasad says:

    great work … Thank u !!!

  24. rajesh says:

    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

  25. Rishik says:

    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

  26. Servaas says:

    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?

Comments are closed for "How to pre-compile JSP?".