JSP 预编译工具

ZDNet软件频道 时间:2008-06-28 作者: | 中国IT实验室 我要评论()
本文关键词:工具 编译 JSP
JSP 预编译工具
 ---------------------------------------------------
  Post Server Startup JSP File Compiler and Validator
  ---------------------------------------------------
  This utility helps to
  1. Simulate a precompilation of JSP's after the server has already started up.
  This way, we do not have to wait for all the JSP's to be precompiled before starting up the server.
  2. Test the validity of all the JSP's. If any JSP does not compile, a response code of 500 is returned. The tool lists that JSP, so it can be worked on.
  
  The list of JSP targets to be precompiled must be supplied in a plain text file.
  
  A sample is below:
  #---------------------------------------------
  /console/index.JSP
  /console/standards/home.JSP
  #---------------------------------------------
  
  All the targets defined in there a hit sequentially, the response code monitored and such information relayed to the user.
  
  Usage:
  java TestJSPFiles <fName>
  
  If you specify no parameters, this help will be displayed
  At least one parameter, the fileName should be specified
  
  Below simulates the default argument list (if no parameters are passed in)JSP-pages.txt
  
  If form based authentication is required, then send the username and parameters
  
  System Properties, passed using the -D flag, are used to set the argument
  list. They are listed below:
  host the hostname or ip address of the machine port the port the server is listening on
  debug If true, extra information like the actual output string is written to std error stream
  If authentication is required to view some of the pages, the following will be required.
  username The username to login as
  password The password for that user formauthtarget The j_security_check target. By default, it is/j_security_check. It can be specified as the login page may vary.
  
  Sample Usage is below:
  java -Ddebug=false -Dhost=localhost -Dport=7701 -Dusername=system -Dpassword=system_password -Dformauthtarget=/console/login/j_security_check TestJSPFiles JSP-pages.txt
  
  At the end, we tell you how many pages were OK (returned 200)
  and how many were broken (including list of broken pages).
  This will help you know which pages are broken immediately.
  
  Any pages already precompiled will not precompile again.
  Thus, there is no performance hit on running this script multiple times.
  
  **** NOTE ****
  The file passed should have the full path of pages to precompile
  Example Scenario:
  webapp context-path is /console
  to precompile the JSP pages, index.JSP and standards/home.JSP in there
  File should be as below
  #---------------------------------------------
  /console/index.JSP
  /console/standards/home.JSP
  
  #---------------------------------------------
  
  Enjoy. - Ugorji ugorji.dick-nwoke@bea.com
  
  // package weblogic.qa.taskmgr;
  
  import java.util.*;
  import java.io.*;
  import java.net.*;
  
  /**
  * Precompiles all the JSP and tell us which are broken
  * <xmp>To see Usage Information: java weblogic.qa.taskmgr.TestJSPFiles</xmp>
  * fName is the list of files with the paths of JSP files to precompile
  * host and port are the server host and port respectively
  *
  * Currently, this is only geared towards FORM-based authenticated sites
  *
  * @author Ugorji Dick-Nwoke ugorji.dick-nwoke@bea.com
  * @version 1.0, August 3, 2001
  */
  public class TestJSPFiles
  {
  private static String HELP_MESSAGE = null;
  
  public boolean DEBUG = false;
  public List JSPFiles;
  public String host;
  public int port;
  public String username;
  public String password;
  public String cookieString;
  public String formAuthTarget = "/j_security_check";
  
  // set the HELP MESSAGE
  static {
  String lsep = System.getProperty ("line.separator");
  HELP_MESSAGE =
  "Usage: " + lsep +
  "java [-Dhost=] [-Dport=] [-Dusername=] [-Dpassword=] [-Dformauthtarget=] TestJSPFiles <fName> " + lsep +
  "Defaults:" + lsep +
  " -Dhost=localhost" + lsep +
  " -Dport=80" + lsep +
  " -Dusername=" + lsep +
  " -Dpassword=" + lsep +
  " -Dformauthtarget=/j_security_check" + lsep +
  " File should be of the form below:" + lsep +
  "#---------------------------------------------" + lsep +
  "/console/index.JSP" + lsep +
  "/console/standards/home.JSP" + lsep +
  "#---------------------------------------------" + lsep +
  "";
  }
  
  public String toString () {
  String s = host + ":" + port + " [user/pass=" + username + "/" + password + "]";
  return s;
  }
  
  public void run ()
  throws Exception
  {
  cookieString = getCookieString ();
  if (DEBUG) System.err.println (cookieString);
  List badFiles = new ArrayList ();
  List goodFiles = new ArrayList ();
  int numGood = 0;
  int numBad = 0;
  
  int sz = JSPFiles.size ();
  for (int i = 0; i < sz; i++) {
  String file = null;
  try {
  file = (String) JSPFiles.get (i);
  int respCode = getResponseCode (file);
  if (respCode == 200) {
  System.out.println ("Good: " + file + " --- Got RespCode " + respCode);
  goodFiles.add (file);
  numGood++;
  }
  else {
  System.out.println ("Error: " + file + " --- Got RespCode " + respCode);
  badFiles.add (file);
  numBad++;
  }
  }
  catch (Exception exc) {
  System.out.println ("Error: " + file + " --- Got Exception " + exc);
  badFiles.add (file);
  numBad++;
  }
  if (DEBUG) {
  System.err.println ("=============================================");
  }
  }
  
  // output stats
  System.out.println ("Good files: " + numGood);
  System.out.println ("Bad files: " + numBad);
  System.out.println ("---------------------------------------");
  for (int i = 0; i < numBad; i++) {
  System.out.println (badFiles.get (i) );
  }
  
  }
  
  public int getResponseCode (String file)
  throws Exception
  {
  int respCode = -1;
  Socket s = new Socket (host, port);
  PrintWriter out = new PrintWriter( new OutputStreamWriter( s.getOutputStream() ) );
  String target = file + "?JSP_precompile=true";
  out.print ("GET " + target + " HTTP/1.0" + "rn");
  out.print ("Cookie: " + cookieString + "rn");
  out.print ("Connection: closern");
  out.print ("Host: " + host + "rn");
  out.print ("rn");
  out.flush ();
  BufferedReader buffreader = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
  String line = buffreader.readLine();
  if( line != null ) {
  StringTokenizer st = new StringTokenizer( line.trim() );
  st.nextToken();
  respCode = Integer.parseInt (st.nextToken());
  }
  while( (line = buffreader.readLine()) != null ) {
  if( line.trim().length() == 0 ) break;
  }
  StringBuffer buf = new StringBuffer ();
  while( (line = buffreader.readLine()) != null ) {
  buf.append (line).append ("n");
  }
  if (DEBUG) {
  System.err.println (target);
  System.err.println (respCode);
  System.err.println (buf.toString () );
  }
  buffreader.close ();
  out.close ();
  s.close ();
  return respCode;
  }
  
  public String getCookieString ()
  throws Exception
  {
  String c

百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134