研究了一下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 哈哈