首页 > java > jsp生成静态页面

jsp生成静态页面

2010年4月23日 发表评论 阅读评论
网站静态化是小型网站提速的最简单实用的方法.本文的代码是使用jsp生成静态页面.
public class toHtml extends HttpServlet
{

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{
String url="";
String name="";
ServletContext sc = getServletContext();

String file_name=request.getParameter("file_name");//你要访问的jsp文件,如index.jsp

//则你访问这个servlet时加参数.?img align=absmiddle src=/images/emot/url.gif>http://localhost/toHtml?file_name=index

url = "/"+file_name+".jsp";//这是你要生成HTML的jsp文件,如

/http://localhost/index.jsp的执行结果.
name="/home/resin/doc/"+file_name+".htm";//这是生成的html文件名,如index.htm.

RequestDispatcher rd = sc.getRequestDispatcher(url);

final ByteArrayOutputStream os = new ByteArrayOutputStream();

final ServletOutputStream stream = new ServletOutputStream()

{

public void write(byte[] data, int offset, int length)

{
os.write(data, offset, length);
}

public void write(int b) throws IOException

{
os.write(b);
}
};

final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

HttpServletResponse rep = new HttpServletResponseWrapper(response)

{

public ServletOutputStream getOutputStream()

{
return stream;
}
public PrintWriter getWriter()
{
return pw;
}
};
rd.include(request, rep);
pw.flush();

FileOutputStream fos = new FileOutputStream(name); //把jsp输出的内容写到xxx.htm

os.writeTo(fos);
fos.close();
PrintWriter out=response.getWriter();

out.print("<p align=center><font size=3 color=red>亲亲宝宝首页已经成功生成</font></p>");

}
}
分类: java 标签: , 2,194 次阅读
原文链接:http://www.wenhq.com/article/view_445.html
欢迎转载,请注明出处:亲亲宝宝
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.