|
How To Precompile Jsp Files
What is JSP CompilationJSP 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 UsersEach 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
Following ANT build file In this snippet from the ANT build file
<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 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 UsersAny ANT task can be executed from within Maven2 .
For example, the following pom.xml 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
|