原理很简单.
虽然一些JSP的WebServer提供预编译的功能,不过也可以试着自己写一个.
里面主要用到了java的HttpURLConnection 和java.io.*;以下是Console的Code:
package sam.bobpig.com;
import java.io.*;
import java.net.*;
/**
* <p>Title: Asp.net 或 Jsp的Precompile程序</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:重庆飞时数码公司 </p>
* @author 宋光义
* @version 1.0
*/
public class Precomplie {
public static FileFilter fileFilter=new PrecompileFileFilter();
public URL rootURL;
public Precomplie(URL aRootURL) {
rootURL = aRootURL;
}
//
public void precompileDirectory(File dir,String startDir)
{
File[] files=dir.listFiles(fileFilter);
for(int i=0;i<files.length;i++)
{
if(files[i].isDirectory())
{
precompileDirectory(files[i],startDir+dir.getName());
}
else
{
precompileFile(files[i],startDir+dir.getName());
}
}
}
public void precompileFile(File file,String startDir)
{
try
{
URL precompileURL=new URL(rootURL,startDir+"/"+file.getName()+"?asp_pre=true");
//System.out.println(rootURL.toString());
System.out.println(precompileURL.toString());
HttpURLConnection conn=(HttpURLConnection) precompileURL.openConnection();
int responseCode=conn.getResponseCode();
System.out.println(rootURL+startDir+"/"+file.getName()+" has ok! ["+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.print("Please supply a base URL and a base directory...");
System.exit(0);
}
try
{
String startURL=args[0];
File start=new File(args[1]);
Precomplie precomp=new Precomplie(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(".aspx")) return true;
return false;
}
}
本文地址:http://com.8s8s.com/it/it13954.htm