jsp生成静态页面的方法

时间:2021-05-26

复制代码 代码如下:
importjava.io.ByteArrayOutputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;

importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpServletResponseWrapper;

publicclasstoHtmlextendsHttpServlet
{

publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException
{
Stringurl="";
Stringname="";

ServletContextsc=getServletContext();

Stringfile_name=request.getParameter("file_name");//你要访问的jsp文件,如index.jsp
//则你访问这个servlet时加参数.如http://localhost/toHtml?file_name=index

url="/"+file_name+".jsp";//这是你要生成HTML的jsp文件,如
//http://localhost/index.jsp的执行结果.

name="/home/resin/resin-2.1.6/doc/"+file_name+".htm";//这是生成的html文件名,如index.htm.

RequestDispatcherrd=sc.getRequestDispatcher(url);

finalByteArrayOutputStreamos=newByteArrayOutputStream();

finalServletOutputStreamstream=newServletOutputStream()
{
publicvoidwrite(byte[]data,intoffset,intlength)
{
os.write(data,offset,length);
}

publicvoidwrite(intb)throwsIOException
{
os.write(b);
}
};

finalPrintWriterpw=newPrintWriter(newOutputStreamWriter(os));

HttpServletResponserep=newHttpServletResponseWrapper(response)
{
publicServletOutputStreamgetOutputStream()
{
returnstream;
}

publicPrintWritergetWriter()
{
returnpw;
}
};
rd.include(request,rep);
pw.flush();
FileOutputStreamfos=newFileOutputStream(name);//把jsp输出的内容写到xxx.htm
os.writeTo(fos);
fos.close();
PrintWriterout=response.getWriter();
out.print("<palign=center><fontsize=3color=red>首页已经成功生成!Andrew</font></p>");
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章