Java Servlet & JSP Cookbook
Problem
You want to convert JSPs into servlets using Tomcat 4.1.x. Solution
Use the JspC command-line tool found in <Tomcat-installation>/bin . Discussion
Using the JspC command-line tool is the first step in precompiling Tomcat JSPs. This tool is offered in the form of a shell script ” jspc.sh on Unix systems and jspc.bat on Windows ”and creates the Java source files for the JSP page implementation classes with which it is supplied. The resultant .java files still have to be compiled into servlet class files, using javac or another Java compiler. Since precompiling JSPs is a two-step process, I recommend a batch file for convenience. However, let's first examine how to use the JspC utility. The Windows shell script for running JspC ( <Tomcat-install-directory>/bin/jspc.bat ) requires that a JASPER_HOME environment variable be set to the Tomcat installation directory. Set this environment variable with the following command line: set JASPER_HOME=k:\jakarta-tomcat-4.1.12 Run the JspC utility by changing to the %JASPER_HOME%\bin directory and typing the following command (specify your own directory paths and issue the command on one line): jspc -d H:\book\cookbook -webinc H:\book\cookbook\map.xml -webapp h:\book\cookbook\dist The -d switch specifies the directory where you would like the source files to be generated, and the -webinc switch specifies the name of an automatically generated file where JspC will create the servlet and servlet-mapping elements for the servlet files. If you compile a JSP page that is called precomp.jsp , the mappings would look like Example 5-1. Example 5-1. Servlet mapping for a precompiled JSP
<servlet> <servlet-name>org.apache.jsp.precomp_jsp</servlet-name> <servlet-class>org.apache.jsp.precomp_jsp</servlet-class> </servlet> <servlet-mapping> <servlet-name>org.apache.jsp.precomp_jsp</servlet-name> <url-pattern>/precomp.jsp</url-pattern> </servlet-mapping> You can then cut and paste these servlet and servlet-mapping elements into the web.xml deployment descriptor for your web application. The -webapp switch specifies a web-application directory, which must in turn have a /WEB-INF subdirectory containing your application's web.xml file. JspC finds all of the .jsp files at the top level of this web-application directory and translates them into servlet source files, along with any JSPs in nested subdirectories. The resulting .java files are placed in the directory specified with the -d switch. Unlike -webinc , the -webxml switch creates an entire web.xml file that includes the new servlets and servlet mappings. Several other JspC options are described here: http://cvs.apache.org/viewcvs/~checkout~/jakarta-tomcat-4.0/jasper/doc/jspc.html. You'll then need to compile the generated source files. I recommend using a batch file to take care of both steps at once. The Windows batch file in Example 5-2 generates the source files and uses the javac tool to compile the servlets. Example 5-2. Using a batch file to precompile JSPs with Tomcat
@echo off jspc -d H:\book\cookbook\classes -webinc H:\book\cookbook\map.xml -webapp h:\book\ cookbook\dist set PRECLASSPATH=%CATALINA_HOME%\common\lib\servlet.jar; %CATALINA_HOME%\common\lib\jasper-runtime.jar;%CLASSPATH% javac -classpath %PRECLASSPATH% -d ./classes *.java Save this file in a text file with a name like precomp.bat . Change to the directory containing the batch file and type precomp . This batch file runs the JspC command on all .jsp files existing beneath the h:\book\cookbook\dist web-application directory. Using the -webinc switch, the command creates an XML fragment of servlet and servlet-mapping elements as shown earlier in this recipe. If there are no problems, the compiled files will be stored in the h:\book\cookbook\classes directory. The code then creates a PRECLASSPATH environment variable that includes the servlet.jar and jasper-runtime.jar components , along with any directories or JARs that are part of the existing CLASSPATH environment variable. The servlet.jar component is necessary to import these Java packages during compilation:
Adding the jasper-runtime.jar is necessary to import the org.apache.jasper.runtime package. On Windows, you may have to set a JASPER_HOME environment variable to the Tomcat installation directory before this batch file runs properly. Example 5-3 shows a Unix shell script that accomplishes the same task. This script executes the jspc.sh file in Tomcat's /bin directory, precompiling all of the JSP files that the JspC tool finds in the current working directory. The script stores the resulting .java files in the ./classes directory. Example 5-3. A shell script for precompiling JSP files
#!/bin/sh $CATALINA_HOME/bin/jspc.sh -d ./classes -webinc ./map.xml -webapp ./; PRECLASSPATH=$CATALINA_HOME/common/lib/servlet.jar:$CATALINA_HOME/common/lib/jasper- runtime.jar; export PRECLASSPATH; javac -classpath $PRECLASSPATH -d ./classes ./classes/*.java See Also
Recipe 5.3 on the precompilation protocol; Recipe 5.4 on mapping the compiled JSP(s) in web.xml ; the JSP precompilation section of JavaServer Pages by Hans Bergsten (O'Reilly); Chapter JSP.11.4 of the JSP 2.0 specification. |