Thursday, June 30, 2011

Tomcat - JSP Precompilation

JSP is usually compiled during runtime by Java server. Some disadvantages are

1. If you JSP page is large, it will take time to compile at runtime. If that is the first hit, the user will have to wait before the page is served to the user. This is a performance bottlenect.

2. Although the current IDE, such as Eclipse, provide JSP syntax check, you may still run into the situation whereby runtime error occurs due to JSP runtime compilation issues.

To overcome these 2 issues, you can precompile your JSP page before putting them into the server. Tomcat come with JSP precompilation tools for your usage.

The following steps provide information on how to perform JSP precompilation

1. Make sure you have your Tomcat server installed. Or, you need the jar files from tomcat/bin and tomcat/lib, as well as tomcat/bin/catalina-tasks.xml. catalina-tasks.xml is a helper file for loading Catalina ant task for you, ie, jasper task.

2. Make sure you have Apache Ant installed

3. Add the following build script. This build script assume that your jsp source is at your web container.

<project name "Webapp Precompilation" default = "all" basedir= ".">
<import file = "${tomcat.home }/bin/catalina-tasks.xml" />
<target name= " jspc">
validateXml = " false"
uriroot= "${webapp.path}"
webXmlFragment= "${webapp path }/WEB-INF/ generat ed we b xml "
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"
srcdir= " $ {webapp.path}/WEB-INF/src"
excludes= " ** /*.smap">
<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}/lib">
<include name= "*.jar" />
<fileset dir= "${tomcat.home}/bin">
<include name= "*.jar" />
<include name= " ** " />
<exclude name= "tags/**" />
</target >
<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"/>
</target >

4. run the script with ant -Dtomcat.home="your tomcat server install home" -Dwebapp.path="your jsp source path". As you can see, tomcat.home is used to locate catalina-tasks.xml and webapp.path is used to locate your libraries and jsp source code. By changing these variables accordingly, you can customize your build path.

5. By default, it will compile your jsp into class file and put them at your "webapp.path"/WEB_INF/classes

Now, how to use these JSP classes file. 2 ways

1. Locate "webapp.path"/WEB_INF/generated_web.xml. Copy these contents into your web.xml

2. Copy all files at "webapp.path"/WEB_INF/classes into your tomcat server work folder. The common path is "your_tomcat_home"/work/Catalina/localhost/_/

You may want to ask why are we not using Ant JSPC task. That task is deprecated due to known problem in Tomcat 1.5 and it won't be fix by Apache Ant as well.

To date, there are 2 known issues for JSP precompilation. below is the abstract from Tomcat

As described in bug 39089, a known JVM issue, bug 6294277, may cause a java.lang.InternalError: name is too long to represent exception when compiling very large JSPs. If this is observed then it may be worked around by using one of the following:
reduce the size of the JSP
disable SMAP generation and JSR-045 support by setting suppressSmap to true.

No comments:

Post a Comment