Topic: 关于用servlet生成静态页面的问题? |
Print this page |
1.关于用servlet生成静态页面的问题? | Copy to clipboard |
Posted by: sun7bear Posted on: 2007-02-01 15:44 package slt; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ToHtml extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); service(request,response); } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void destroy() { } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = ""; String name = ""; String pName = ""; ServletContext sc = getServletContext(); url = "/indexbak125.jsp"; //这是要生成HTML的jsp文件,如//http://localhost/index.jsp的执行结果. name = "indexa.html"; //这是生成的html文件名,如index.htm. pName = "/usr/tomcat/webapps/ROOT/indexa.html"; //生成html的完整路径 RequestDispatcher rd = sc.getRequestDispatcher(url); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final ServletOutputStream stream = new ServletOutputStream() { public void write(byte[] data, int offset, int length) { os.write(data, offset, length); } public void write(int b) throws IOException { os.write; } }; final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os)); HttpServletResponse rep = new HttpServletResponseWrapper(response) { public ServletOutputStream getOutputStream() { return stream; } public PrintWriter getWriter() { return pw; } }; rd.include(request, rep); pw.flush(); FileOutputStream fos = new FileOutputStream(pName); //把jsp输出的内容写到指定路径的htm文件中 os.writeTo(fos); fos.close(); response.sendRedirect(name); //书写完毕后转向htm页面 } } 我用这个servlet生成了静态页面,可为什么生成出来的静态页面是乱码,而且还不能转向呢? |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |