[ Team LiB ] |
![]() ![]() |
Precompiling JSPsConverting a JSP into a servlet and then compiling the servlet can be a time-consuming task for a JSP engine. Although the compilation happens only when the page has changed, you might want to compile all the pages ahead of time to make sure your users never see any delay when accessing recently changed JSPs. The JSP specification includes a special precompilation directive that asks the JSP engine to precompile a JSP rather than execute it. You just need to send a jsp_precompile="true" parameter or an empty jsp_precompile parameter to tell the JSP engine that you want it to precompile a page. Listing 24.1 shows a program that takes a base URL and a base directory and asks the JSP engine to precompile every JSP in the directory, including those in subdirectories. You can use this utility to eliminate compilation delays and also to locate JSPs that no longer compile cleanly. Because you normally see compile errors only when you access a page, you often don't discover a broken JSP until a user stumbles upon it, especially when it's a rarely used page. The program in Listing 24.1 helps you locate those pages before the user does. Listing 24.1 Source Code for Precompile.javapackage usingjsp; import java.io.*; import java.net.*; /** A class to recursively visit JSP files and precompile them using * a JSP engine */ public class Precompile { public static FileFilter fileFilter = new PrecompileFileFilter(); public URL rootURL; public Precompile(URL aRootURL) { rootURL = aRootURL; } /** Precompiles an entire directory of files */ public void precompileDirectory(File dir, String startDir) { // Get all the files that are either directories or JSP files (the filter // source code is at the bottom of this file) File[] files = dir.listFiles(fileFilter); for (int i=0; i < files.length; i++) { // If the current file is a directory, precompile its contents if (files[i].isDirectory()) { precompileDirectory(files[i], startDir+"/"+dir.getName()); } else { // Otherwise precompile the current file precompileFile(files[i], startDir+"/"+dir.getName()); } } } public void precompileFile(File file, String startDir) { try { // Create the URL for precompiling URL precompileURL = new URL(rootURL, startDir+"/"+ file.getName()+"?jsp_precompile=true"); // Create a URL connection to the JSP HttpURLConnection conn = (HttpURLConnection) precompileURL. openConnection(); // Check the response code (this also forces the connection to contact // the Web server and precompile the JSP) int responseCode = conn.getResponseCode(); System.out.println(startDir+file.getName()+": "+responseCode); } catch (Exception exc) { System.out.println("Exception: "+exc.toString()+ " for "+startDir+file.getName()); } } public static void main(String[] args) { if (args.length < 2) { System.out.println( "Please supply a base URL and a base directory"); System.exit(0); } try { String startURL = args[0]; File start = new File(args[1]); Precompile precomp = new Precompile(new URL(startURL)); if (start.isDirectory()) { precomp.precompileDirectory(start, ""); } else { precomp.precompileFile(start, ""); } } catch (Exception exc) { exc.printStackTrace(); } } } class PrecompileFileFilter implements FileFilter { public boolean accept(File f) { if (f.isDirectory()) return true; if (f.getName().endsWith(".jsp")) return true; return false; } } ![]() |
[ Team LiB ] |
![]() ![]() |