jsp动态生成静态页面
研究了一下动态生成静态页面 废话不多说了 直接上代码
java代码
/**
* @author jeedroid
* @time 2011-11-24 00:47:52
*/
package com.jeedroid.makehtml;
import java.io.*;
import java.net.malformedurlexception;
import java.net.url;
import java.net.urlconnection;
import java.net.urlencoder;
public class makehtml
{
public static void makehtml(string url)
{
try {
//读取要访问的文件的内容
url _url=new url(url);
urlconnection conn=_url.openconnection();
inputstream inputstream=conn.getinputstream();
bufferedreader buffer=new bufferedreader(new inputstreamreader(inputstream));
stringbuilder stringbuilder=new stringbuilder();
string line=null;
while((line=buffer.readline())!=null)
{
stringbuilder.append(line);
}
string filecontent=stringbuilder.tostring();
inputstream.close();
string htmlfilename=generatefilename(url);
file htmlfile=new file("dhtml/",htmlfilename);
if(!htmlfile.exists())
{
htmlfile.createnewfile();
//把filecontent写入到html文件中
filewriter filewriter=new filewriter(htmlfile,true);
bufferedwriter bufferedwriter=new bufferedwriter(filewriter);
bufferedwriter.write(filecontent);
bufferedwriter.close();
filewriter.close();
}
} catch (malformedurlexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
//generate html filename according to request url
public static string generatefilename(string url)
{
string prefilename=url.substring(url.lastindexof("/"));
//需要把请求参数中的"?"转换一下 否则文件名不能出现问号
string filename=prefilename.replace("?", urlencoder.encode("?"));
return filename.replace(".", "_")+".html";
}
}
/**
* @author jeedroid
* @time 2011-11-24 00:47:52
*/
package com.jeedroid.makehtml;
import java.io.*;
import java.net.malformedurlexception;
import java.net.url;
import java.net.urlconnection;
import java.net.urlencoder;
public class makehtml
{
public static void makehtml(string url)
{
try {
//读取要访问的文件的内容
url _url=new url(url);
urlconnection conn=_url.openconnection();
inputstream inputstream=conn.getinputstream();
bufferedreader buffer=new bufferedreader(new inputstreamreader(inputstream));
stringbuilder stringbuilder=new stringbuilder();
string line=null;
while((line=buffer.readline())!=null)
{
stringbuilder.append(line);
}
string filecontent=stringbuilder.tostring();
inputstream.close();
string htmlfilename=generatefilename(url);
file htmlfile=new file("dhtml/",htmlfilename);
if(!htmlfile.exists())
{
htmlfile.createnewfile();
//把filecontent写入到html文件中
filewriter filewriter=new filewriter(htmlfile,true);
bufferedwriter bufferedwriter=new bufferedwriter(filewriter);
bufferedwriter.write(filecontent);
bufferedwriter.close();
filewriter.close();
}
} catch (malformedurlexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
//generate html filename according to request url
public static string generatefilename(string url)
{
string prefilename=url.substring(url.lastindexof("/"));
//需要把请求参数中的"?"转换一下 否则文件名不能出现问号
string filename=prefilename.replace("?", urlencoder.encode("?"));
return filename.replace(".", "_")+".html";
}
}
然后用junit做了下测试
java代码
package com.jeedroid.makehtml;
import static org.junit.assert.*;
import org.junit.test;
public class makehtmltest
{
@test
public void testmakehtml()
{
makehtml.makehtml("https://localhost:8080/dynamictohtml/index.jsp");
}
}
package com.jeedroid.makehtml;
import static org.junit.assert.*;
import org.junit.test;
public class makehtmltest
{
@test
public void testmakehtml()
{
makehtml.makehtml("https://localhost:8080/dynamictohtml/index.jsp");
}
}
还不错 直接生成了静态页面 访问的时候只要用urlrewriter把对index.jsp的请求导向servlet,然后调用上面的javabean,转向html页面就行了 只要url一样就直接显示那个html 哈哈
上一篇: 制作我们自己的Ebay(拍卖系统)(4)