JSP Pre-compile tool

Source: Internet
Author: User
Tags add flush integer socket string tostring trim stringbuffer
js| compilation
A program that can be used for JSP precompiled work. The following are read.me and Java source programs.
Author:ugorji Dick-nwoke ugorji.dick-nwoke@bea.com
Date:jan 16. 2002

---------------------------------------------------
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 don't have to, and the jsp´s to is precompiled
Before starting up the server.
2. Test the validity of the jsp´s. If any JSP does not compile, a
Response code is returned. The tool lists that JSP, so it can
is worked on.

The list of JSP targets to is precompiled must is supplied in a plain
Text file.

A sample is below:
#---------------------------------------------
/console/index.jsp
/console/standards/home.jsp
#---------------------------------------------

All of 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 are specify no parameters, this help would be displayed
At least one parameter, the FileName should is specified

Below simulates the default argument list (if no parameters are 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
are written to STD error stream
If authentication is required to view some of the pages, the following
would 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 your how many pages were OK (returned 200)
And how many were broken (including list of broken pages).
This'll help you know which pages are broken immediately.

Any pages already precompiled won't 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 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 of the JSP and tell us which are broken
* <xmp>to 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 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" + "");
Out.print ("Cookie:" + cookiestring + "");
Out.print ("Connection:close");
Out.print ("Host:" + Host + "");
Out.print ("");
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 ("");
}
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 cookieline = null;
String c = null;
Socket s = new socket (host, port);
PrintWriter out = new PrintWriter (New OutputStreamWriter (S.getoutputstream ()));
String target = Formauthtarget;
out = new PrintWriter (System.out, true);
Out.print ("POST" + target + "http/1.0" + "");
Out.print ("Connection:close");
Out.print ("Host:" + Host + "");
Out.print ("content-type:application/x-www-form-urlencoded");
String poststring = "J_username=" + username + "&j_password=" + password;
Out.print ("content-length:" + poststring.length () + "");
Out.print ("");
Out.print (poststring + "");
Out.print ("");
Out.flush ();
BufferedReader buffreader = new BufferedReader (New InputStreamReader (S.getinputstream ()));
String line = null;
while (line = Buffreader.readline ())!= null) {
if (DEBUG) System.err.println (line);
if (Line.trim (). Length () = 0) break;
if (Line.tolowercase (). StartsWith ("Set-cookie:")) {
Cookieline = line.substring ("Set-cookie:". Length ()). Trim ();
int semicolon = Cookieline.indexof (";");
if (semicolon!=-1) {
Cookieline = cookieline.substring (0, semicolon). Trim ();
}
Break
}
}
Buffreader.close ();
Out.close ();
S.close ();
return cookieline;
}

public int getResponseCode0 (String file)
Throws Exception
{
HttpURLConnection urlconn = null;
try {
String target = file + "? Jsp_precompile=true";
URL url = new URL ("http", host, port, target);
Urlconn = (httpurlconnection) url.openconnection ();
Urlconn.connect ();
InputStream is = Urlconn.getinputstream ();
while (Is.read (b)!=-1) {}
int respcode = Urlconn.getresponsecode ();
return respcode;
}
finally {
try {if (urlconn!= null) Urlconn.disconnect ();}
catch (Exception exc2) {}
}
}

/** <xmp>usage:java weblogic.qa.taskmgr.TestJSPFiles <fName> public static void Main (string[] args)
Throws Exception
{
if (Args.length = = 0) {
System.out.println (Help_message);
System.exit (0);
}

String fName = "Jsp-pages.txt";
String thost = "localhost";
int tport = 80;
String tUser = null;
String tpass = null;
String tformauthtarget = "/j_security_check";
Boolean tdebug = false;

String tmpstr = null;
if ((Tmpstr = System.getproperty ("host"))!= null)
Thost = TMPSTR;
if ((Tmpstr = System.getproperty ("port"))!= null)
Tport = Integer.parseint (TMPSTR);
if ((Tmpstr = System.getproperty ("username"))!= null)
TUser = TMPSTR;
if ((Tmpstr = System.getproperty ("password"))!= null)
Tpass = TMPSTR;
if ("true". Equals (System.getproperty ("Debug"))
Tdebug = true;
if ((Tmpstr = System.getproperty ("Formauthtarget"))!= null)
Tformauthtarget = TMPSTR;

if (Args.length > 0)
FName = args [0];
if (Args.length > 1)
Thost = args [1];
if (Args.length > 2)
Tport = Integer.parseint (args [2]);
if (Args.length > 3)
TUser = args [3];
if (Args.length > 4)
Tpass = args [4];

FileReader FR = new FileReader (fName);
BufferedReader br = new BufferedReader (FR);
List files = new ArrayList ();
String line = null;
while (line = Br.readline ())!= null) {
line = Line.trim ();
if (line.length () = = 0 | | line.startswith ("#"))
Continue
Files.add (line);
}
Fr.close ();
Br.close ();

Testjspfiles test = new Testjspfiles ();
Test.host = Thost;
Test.port = Tport;
Test.jspfiles = files;
Test.username = TUser;
Test.password = Tpass;
Test.formauthtarget = Tformauthtarget;
Test. DEBUG = Tdebug;

System.out.println ("File&:" + fName);
System.out.println ("Testjspfiles:" + test);
Test.run ();

}


}

# Top level files
/console/index.jsp
/console/help.jsp

# Other Stuff
/webapp1/thankyou.jsp
/webapp2/thankyou2.jsp
 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.