|
8.7. Compiling JSPsWhen deploying to servers, the jspc task can be useful. This task runs the JavaServer Pages compiler and turns JSP pages into Java source, which you can compile with the javac task. Compiling JSP pages supports fast invocation of JSP pages, deployment on servers without the complete JDK, or lets you check the syntax of pages without deploying them. By default, this task uses the Jasper JSP compiler, which comes with Tomcat. Copy Tomcat's jasper-compiler.jar and jasper-runtime.jar files into the Ant lib directory to use this task. You'll need servlet.jar, which comes with Tomcat, in the Ant lib directory. For example, say you have this JSP page, greeting.jsp: <HTML> <HEAD> <TITLE>Creating a Greeting</TITLE> </HEAD> <BODY> <H1>Creating a Greeting</H1> <% out.println("Hello from JSP!"); //Display the greeting %> </BODY> </HTML> Example 8-5 shows how to compile this JSP into greeting.java. Example 8-5. Compiling JSP pages (ch08/jspc/build.xml)<?xml version="1.0" ?> <project default="main"> <property name="message" value="Compiling the JSP...." /> <property name="src" location="source" /> <property name="output" location="bin" /> <target name="main" depends="init, compile"> <echo> ${message} </echo> </target> <target name="init"> <mkdir dir="${output}" /> </target> <target name="compile"> <jspc srcdir="${src}" destdir="${output}" package="org.antbook.jsp" verbose="9"> <include name="**/*.jsp" /> </jspc> </target> </project> Here's what you see when you run the build file: %ant Buildfile: build.xml init: compile: [jspc] Compiling 1 source file to /home/steven/ant/ch08/jspc/bin/org/antbook/jsp [jasperc] 2004-06-30 02:20:09 - Class name is: greeting [jasperc] 2004-06-30 02:20:09 - Java filename is: /home/steven/ant/ch08/jspc/bin/org/antbook/jsp/greeting.java [jasperc] 2004-06-30 02:20:09 - Accepted org.apache.jasper.compiler.Parser$Scriptlet at /greeting.jsp(7,4) [jasperc] 2004-06-30 02:20:09 - Compiling with: -encoding UTF8 main: [echo] [echo] Compiling the JSP.... [echo] BUILD SUCCESSFUL Total time: 3 seconds And this is what the resulting Java code, greeting.java, looks like: package org.antbook.jsp.; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import org.apache.jasper.runtime.*; public class greeting extends HttpJspBase { static { } public greeting( ) { } private static boolean _jspx_inited = false; public final void _jspx_init( ) throws org.apache.jasper.runtime.JspException { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; . . . out.println("Hello from JSP!"); //Display the greeting . . . } } Here's another example, which compiles JSP pages, checks the dependencies, and uses the javac task to create the actual bytecode for this JSP: <jspc destdir="temp" srcdir="${src}" package="org.antbook.ch08"> <include name="**/*.jsp" /> </jspc> <depend srcdir="temp" destdir="${bin}" classpath="lib/app.jar"/> <javac srcdir="temp" destdir="${bin}" classpath="lib/app.jar" /> You can see the attributes of the jspc task in Table 8-5.
The jspc task is a directory-based task, so the JSP files to be compiled are located the same way as the javac task locates files, which means you can use nested elements, such as includes and excludes. You can use nested classpath and classpathref elements, as well as nested webapp elements. The webapp element, unique to the jspc task, instructs the JSP compiler to build an entire Web application. The one attribute of the webapp element is the basedir attribute, which sets the base directory of the application. The base directory must have a WEB-INF subdirectory beneath it. If you use this element, the task uses the compiler for all dependency checking. Here's an example using webapp: <jspc package="org.antbook.ch08"> <include name="**/*.jsp" /> <webapp basedir="${ch08}" /> </jspc> |
|