Section 8.7. Compiling JSPs


8.7. Compiling JSPs

When 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.

Table 8-5. The jspc task's attributes

Attribute

Description

Required

Default

classpath

Specifies the classpath to use when you're running the JSP compiler. You can specify this by nested classpath elements.

No

 

classpathref

Specifies the classpath to use when you're running the JSP compiler, by reference.

No

 

compiler

Specifies the classname of a JSP compiler, such as jasper or jasper42.

No

jasper

compilerclasspath

Specifies the classpath that should be used to find the compiler specified by the compiler attribute.

No

 

destdir

Specifies where you want to place the compiled files.

Yes

 

failonerror

Specifies if the task to fail if it encounters an error.

No

yes

ieplugin

Specifies the Java Plugin class ID for Internet Explorer.

No

 

mapped

Specifies you want to generate separate write statements for each HTML line in the JSP. Set to true/false.

No

 

package

Specifies the name of the destination package for the generated compiled classes.

No

 

srcdir

Specifies where you want the task to look for source JSP files.

Yes

 

uribase

Specifies the base URI for relative URI references in JSP pages.

No

 

uriroot

Sets the root directory from which URIs should be resolved.

No

 

verbose

Specifies you want full (verbose) output. Set to an integer value (0-9).

No

0

webinc

Specifies the filename for the section of web.xml that details any servlets.

No

 

webxml

Specifies the filename for the generated web.xml-type file.

No

 


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>



    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net