复制代码 代码如下:
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>");
}
}