What is JSP Compilation

JSP compilation is a process of turning JSP pages into class files which a JVM can run. Servlet Containers usually compile JSP pages on first access. Compilation step is preceded by translation phase where JSP pages are turned into Servlet classes.

Why should Dekoh applications ship compiled JSP Files?

Dekoh Server will compile JSP pages on first access if the machine has JDK (javac) installated. This works well for developement scenarios where developers have JDK installed. Developers can modify the JSPs and refresh the browser to view the changed jsp.

But Dekoh applications are intended to also run on user desktops that have only a JRE installation. Since such computers do not have javac, Dekoh applications must be distributed with pre-compiled JSP files.

How to compile JSP Files?

You can integrate the step of compiling the JSPs into the build process that creates the distributable (WAR) file of your application.

ANT Users

Each Dekoh installation includes http://ant.apache.org/ ANT tasks that can help you compile JSP files.

Apart from compiling the JSP files Dekoh server supports instrumenting the JSP file with http://jcp.org/en/jsr/detail?id=45 enable JSR-45 compliant debugging information. This means that when an exception is thrown while executing a JSP page, the line number of the JSP source will be inserted in the stacktrace. This instrumentation also enables JSR-45 compliant Tools/IDEs to attach a debugger that can step through your JSP.

  • com.pramati.tools.ant.AntTaskHandlerJSPCompiler is the ANT task that will translate JSP pages into Servlet Java files.
  • com.pramati.tools.ant.AntTaskHandlerInstrument is the ANT task that will instrument the compiled JSP pages with debugging information.

Following ANT build file(info), along with jsp-compile.props(info) show an example of how to use these ANT tasks are used to compile the JSP pages in your application.

In this snippet from the ANT build file(info), the JSP files in the directory appRoot are first translated to Servlet Java files which are stored at WEB-INF/jsp_src. These generated java files are then compiled using the standard javac task. The compile class files are then instrumented with JSR-45 compliant debugging information.

 	   <target name="translate" depends="verify.setup">
	        <jspc appRoot="${SERVLET_ROOT}" validateXML="true"
	              targetWebXmlPath="${web.xml.file}" includes="${includes.jsp.list}" excludes="${exclude.jsp.list}" actualWebXmlPath="${web.xml.file}"
	              outputDir="${SERVLET_ROOT}/WEB-INF/jsp_src">
	            <classpath>
	                <pathelement path="${project.class.path}"/>
	            </classpath>
	        </jspc>
	    </target>
	    <target name="compile" depends="translate">
 	        <mkdir dir="${SERVLET_ROOT}/WEB-INF/classes"/>
	        <mkdir dir="${SERVLET_ROOT}/WEB-INF/lib"/>
	        <javac destdir="${SERVLET_ROOT}/WEB-INF/classes" optimize="off" debug="on" failonerror="false"
	               srcdir="${SERVLET_ROOT}/WEB-INF/jsp_src" memoryinitialsize="256m" memorymaximumsize="512m" fork="yes">
	            <classpath>
	                <pathelement path="${project.class.path}"/>
	            </classpath>
 	        </javac>
	    </target>
 	    <target name="instrument" depends="compile">
	        <instrument srcDir="${SERVLET_ROOT}/WEB-INF/jsp_src" classDir="${SERVLET_ROOT}/WEB-INF/classes"/>
	    </target>

Save the build xml file(info) and the properties file(info) in your application's root directory. Running ANT to executing the default target in the build file will compile all the JSP files that are found under application Root directory (including the sub-directo ies). You may have to modify the value of property project.class.path to include all the classes that the JSP files depend on.

ant -f <full path to jsp-compile.xml>

These compiled classes will be put in WEB-INF/classes folder. The application's web.xml will be modified, to insert a servlet-mapping for each of the compiled JSP.

For example if gameOfEight.jsp is compiled, following entries will be made in the web.xml:

   <servlet>
      <servlet-name>gameofeight.jsp</servlet-name>
      <display-name>gameofeight.jsp</display-name>
      <servlet-class>com.pramati.jsp.generated.gameofeight_p2e_jsp</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>gameofeight.jsp</servlet-name>
      <url-pattern>gameofeight.jsp</url-pattern>
   </servlet-mapping>

Because of this mapping any access to gameofeight.jsp is mapped to execute the generated servlet class com.pramati.jsp.generated.gameofeight_p2e_jsp. This modified web.xml should be packaged along with the compiled JSP classes in WEB-INF/classes into a WAR file that can be distributed.

Following is the structure of distributable gameOfEight.war :

     0 Thu Sep 20 11:21:38 IST 2007 WEB-INF/classes/com/
     0 Thu Sep 20 11:21:38 IST 2007 WEB-INF/classes/com/pramati/
     0 Thu Sep 20 11:21:38 IST 2007 WEB-INF/classes/com/pramati/jsp/
     0 Thu Sep 20 11:55:20 IST 2007 WEB-INF/classes/com/pramati/jsp/generated/
  8365 Thu Sep 20 11:55:20 IST 2007 WEB-INF/classes/com/pramati/jsp/generated/gameofeight_p2e_jsp.class
     0 Thu Sep 20 11:21:40 IST 2007 WEB-INF/classes/gameofeights/
  3036 Thu Sep 20 11:21:40 IST 2007 WEB-INF/classes/gameofeights/Eight.class
   861 Thu Sep 20 11:55:18 IST 2007 WEB-INF/web.xml

Maven Users

Any ANT task can be executed from within Maven2.

For example, the following pom.xml(info) is executing the ANT build file specified above.

To run this, execute command - mvn install.

On Linux machines, you may have to modify the jsp-compile.props to specify value for property install.root and user.name.

Attachments

jsp-compile.props Info on jsp-compile.props 488 bytes
jsp-compile.xml Info on jsp-compile.xml 2124 bytes
pom.xml Info on pom.xml 796 bytes