1.在 index.jsp文件中,加入一条超链接,用来访问将 Jsp 转换成Html的servlet
<a href="servlet/GeneraticServlet">把template文件夹下的jsp都生成静态html文件</a>
2. 在项目根目录下新建一个template文件夹,将要转换成静态html的 jsp页面放入,我新建的的文件为MyJsp1.jsp, MyJsp2.jsp, MyJsp3.jsp, 内容类似
<body>
这是第<%=1 %> 个jsp页面 <br>
</body>
3. GeneraticServlet这个servlet中使用 RequestDispatcher 访问多个Jsp
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//将Jsp页面转换成html,存储在项目路径下,source为要转换的jsp, target为生成的html文件名
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/servlet/JspToHtml?source=MyJsp1.jsp&target=1.html");
//发送request的请求,请求成功后,程序返回到此处继续执行,这是用dispatcher.include()方法的主要原因,可以发出多次reqeust的请求
dispatcher.include(request, response);
//生成第二个html
dispatcher = getServletContext().getRequestDispatcher("/servlet/JspToHtml?source=MyJsp2.jsp&target=2.html");
dispatcher.include(request, response);
//生成第三个html
dispatcher = getServletContext().getRequestDispatcher("/servlet/JspToHtml?source=MyJsp3.jsp&target=3.html");
dispatcher.include(request, response);
response.sendRedirect("../index.jsp?responsecode=1");
}
JspToServelt的代码如下
response.setContentType("text/html;charset=utf-8");
String source =request.getParameter("source");
String target = request.getParameter("target");
try {
//获取jsp响应内容,第一个参数为文件路径
String content = getJspOutput("../template/"+source,request,response);
//将得到的响应内容写入一个html文件中
//获得项目根目录地址
String rootPath = this.getServletConfig().getServletContext().getRealPath("/");
//创建文件
File file = new File(rootPath+target);
if(!file.exists())
{
file.createNewFile();
}
else
{
//先删除原文件
file.delete();
file.createNewFile();
}
//在文件中写入内容
FileOutputStream fos = new FileOutputStream(rootPath+target);
fos.write(content.getBytes());
fos.flush();
fos.close();
System.out.println(file.getAbsolutePath()+"生成成功");
5 注意到, getJspOutput ( "../template/" +source ,request ,response ) 改方法也是自己定义的,第一个参数为要转换的jsp文件的路径
public static String getJspOutput(String jsppath, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
WrapperResponse wrapperResponse = new WrapperResponse(response);
//浏览器显示的数据都是response流输出的(response.getWriter().print())
//所以可以通过修改response的getWriter()方法,将文件输出流传入PrintWriter中
//getWriter方法是容器自己调用的,我们只需要重写 wrapperResponse的getWriter方法就可以
request.getRequestDispatcher(jsppath).include(request, wrapperResponse);
//获取自定义文件流的内容
return wrapperResponse.getContent();
}
6. WrapperResponse 自己定义的,继承了 HttpServletResponseWrapper ,用来包装HttpServletResponse
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class WrapperResponse extends HttpServletResponseWrapper {
private MyPrintWriter tmpWriter;
private ByteArrayOutputStream output;
public WrapperResponse(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
output = new ByteArrayOutputStream();
tmpWriter = new MyPrintWriter(output);
}
public void finalize() throws Throwable {
super.finalize();
output.close();
tmpWriter.close();
}
public String getContent() {
try {
tmpWriter.flush(); //刷新该流的缓冲,详看java.io.Writer.flush()
String s = tmpWriter.getByteArrayOutputStream().toString("utf-8");
//此处可根据需要进行对输出流以及Writer的重置操作
//比如tmpWriter.getByteArrayOutputStream().reset()
return s;
} catch (UnsupportedEncodingException e) {
return "UnsupportedEncoding";
}
}
//覆盖getWriter()方法,使用我们自己定义的Writer
public PrintWriter getWriter() throws IOException {
return tmpWriter;
}
public void close() throws IOException {
tmpWriter.close();
}
//自定义PrintWriter,为的是把response流写到自己指定的输入流当中
//而非默认的ServletOutputStream
private static class MyPrintWriter extends PrintWriter {
ByteArrayOutputStream myOutput; //此即为存放response输入流的对象
public MyPrintWriter(ByteArrayOutputStream output) {
super(output);
myOutput = output;
}
public ByteArrayOutputStream getByteArrayOutputStream() {
return myOutput;
}
}
}
因为浏览器显示的数据都是response流输出的(response.getWriter().print())
所以可以通过修改response的getWriter()方法,将文件输出流传入PrintWriter中
getWriter方法是容器自己调用的,我们只需要重写 wrapperResponse的getWriter方法就可以
完整的项目可以下载:http://download.csdn.net/detail/ch717828/8393449
参考文章:http://blog.csdn.net/guo_rui22/article/details/2170750
http://xifangyuhui.iteye.com/blog/840518
http://pignut-wang.iteye.com/blog/104096