jsp生成静态页面的方法
jsp生成静态页面的方法
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下:importjava.io.ByteArrayOutputStream;importjava.io.FileOutputS...

复制代码 代码如下:

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>");

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新JSP教程学习
热门JSP教程学习
编程开发子分类