今天小编给大家分享一篇jsp生成静态页面的详细教程,感兴趣的朋友跟小编一起来了解一下吧!
复制代码 代码如下:
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("
");
}
}
以上就是jsp生成静态页面的详细教程,想必都了解了吧,更多相关内容请继续关注错新技术频道。
新闻热点
疑难解答