Recipe 5.2 Precompiling a JSP in WebLogic


Problem

You want to precompile a JSP in WebLogic.

Solution

Use the weblogic.jspc Java utility that installs with WebLogic Server 7.0.

Discussion

WebLogic Server 7.0 installs with its own Java utility for precompiling JSPs: weblogic.jspc . This utility is part of the JAR file that can be found at this location: <WebLogic-install-directory>/weblogic700/server/lib/weblogic.jar . When you precompile JSPs using weblogic.jspc , it places the class files in the specified destination directory. Example 5-4 shows a simple batch file on Windows NT that precompiles an example.jsp JSP page into its servlet implementation class.

Example 5-4. Precompiling a JSP with weblogic.jspc
 @echo off set WLCLASSPATH=k:\bea\weblogic700\server\lib\weblogic.jar;%CLASSPATH%  java -cp %WLCLASSPATH% weblogic.jspc -d .\classes example.jsp 

The second line of Example 5-4 sets an environment variable, WLCLASSPATH . This variable prepends a reference to weblogic.jar to the existing CLASSPATH variable. The next line of the example uses this combined classpath to run weblogic.jspc . The -d switch tells the program where to store the resulting class files, in this case, in the classes directory beneath the directory containing the batch file and example.jsp . This program generates a Java class file named jsp_servlet._ _example.class (including the package name ). If you do not specify a package for the compiled servlet, jsp_servlet is used as the default package name (see Example 5-6). Example 5-5 shows a shell script that is written on Mac OS X for precompiling a JSP with WebLogic.

Example 5-5. Precompiling JSPs with weblogic.jspc and a shell script
 #!/bin/sh WLCLASSPATH=/Users/bruceper/java/weblogic_jar/weblogic.jar:$CLASSPATH; export WLCLASSPATH; java -cp $WLCLASSPATH weblogic.jspc   -d /Users/bruceper/books/cookbook/code/chap5/classes  newfile.jsp 

weblogic.jspc is different from Tomcat's JspC utility in that it compiles a file in JSP page syntax into the servlet class file in a single operation. Using Tomcat's JspC from the command line requires the use of a compiler, such as javac , to compile the .java files generated by JspC into class files. This second compilation step when using JspC is handled automatically when using weblogic.jspc .


The Windows batch file in Example 5-6 specifies a jspservletcookbook package for all the JSP pages found in the web application specified by the -webapp switch.

Example 5-6. Using weblogic.jspc to precompile all JSP pages in a web application
 @echo off set WLCLASSPATH=k:\bea\weblogic700\server\lib\weblogic.jar;%CLASSPATH% java -cp %WLCLASSPATH% weblogic.jspc -d .\classes -package jspservletcookbook -compileAll  -webapp h:/home 

Example 5-7 shows a Unix shell script that does the same thing.

Example 5-7. Precompiling all JSP pages in a web application with a shell script
 #!/bin/sh WLCLASSPATH=/Users/bruceper/java/weblogic_jar/weblogic.jar:$CLASSPATH; export WLCLASSPATH; java -cp $WLCLASSPATH weblogic.jspc -d /Users/bruceper/books/cookbook/code/chap5/classes -package jspservletcookbook -compileAll -webapp /Users/bruceper/books/cookbook/code/chap5 

Note this portion of the instruction in the example:

 -compileAll -webapp h:/home 

The -compileAll switch, along with an argument to -webapp , tells weblogic.jspc to precompile all the JSP files found in the web application configured in the h:\home directory, including any JSP files nested in subdirectories. This web application is in exploded directory format (not archived into a WAR file). In Example 5-6, the compiled classes are stored in the \classes\jspservletcookbook directory path .

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.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

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