jsp动态生成静态页面
研究了一下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("http://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("http://localhost:8080/DynamicToHtml/index.jsp");
}
}
还不错 直接生成了静态页面 访问的时候只要用urlrewriter把对index.jsp的请求导向servlet,然后调用上面的javabean,转向html页面就行了 只要url一样就直接显示那个html 哈哈