Precompile JSP pagesTag(s): Servlet/JSP

Some JSP containers (as per section 8.4.2 of the JSP 1.2 specification) support the capability of precompiling a JSP page.

To precompile a JSP page, access the page with a query string of ?jsp_precompile

The JSP page will not be executed. If the container supports precompilation, the JSP page will be compiled if necessary.

Here a JSP page that will scan the current directory (and subdirectories) to precompile all JSP found.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.jsp.*"%>
<%@ page import="java.util.Set,java.util.Iterator,"%>

<%! private void compileAllJsps
     (PageContext pageContext, JspWriter out,
      HttpServletRequest request,HttpServletResponse response, 
      String uripath)
         throws IOException, ServletException {

     Set set = pageContext.getServletContext().getResourcePaths(uripath);
     for (Iterator iter = set.iterator(); iter.hasNext();) {
         String uri = (String);
         if (uri.endsWith(".jsp")) {
             out.write("<li>"+ uri +"</li>");
             RequestDispatcher rd = 
             if (rd == null) {
                 throw new Error(uri +" - not found");
             rd.include(request, response);
         else if (uri.endsWith("/")) {
             compileAllJsps(pageContext, out, request, response, uri); 

<html><head><title>Precompiling *.JSPs</title></head>
<body><h4>Precompiling *.JSPs:</h4>
  HttpServletRequest req = new HttpServletRequestWrapper(request) {
      public String getQueryString() {
          // can be "jsp_precompile=true"
          return "jsp_precompile";
  compileAllJsps(pageContext, out, req, response, "/");
</body> </html>

NOTE: Many Application servers provide an utility to precompile JSP pages (ex. EAServer, BEAWLS). Check for a JSPC command file.

blog comments powered by Disqus