日期:2014-05-16 浏览次数:20393 次
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //实际中这个路径会根据参数在一定规则的计算下得到,保证其唯一性 File f = new File("/htmls/test.html"); if (!f.exists()) { //加工数据,实际中会调用service request.setAttribute("name", "测试"); String content = JspUtil.getHtml(request, response, "/index.jsp"); FileUtil.save(content, "/htmls", null, "test.html"); } StringBuffer sb = new StringBuffer(); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String str = null; while ((str = br.readLine()) != null) { sb.append(str); } fr.close(); br.close(); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.print(sb.toString()); out.flush(); }
public class JspUtil { public static String getHtml(HttpServletRequest request, HttpServletResponse response, String jsp) throws ServletException, IOException { FileCaptureResponseWrapper fileCaptureResponseWrapper = new FileCaptureResponseWrapper(response); request.getRequestDispatcher(jsp).forward(request, fileCaptureResponseWrapper); return fileCaptureResponseWrapper.toString(); } public static class FileCaptureResponseWrapper extends HttpServletResponseWrapper { private CharArrayWriter writer; public FileCaptureResponseWrapper(HttpServletResponse response) { super(response); this.writer = new CharArrayWriter(); } public PrintWriter getWriter() { return new PrintWriter(writer); } @Override public String toString() { String result = writer.toString(); writer.flush(); writer.close(); return result; } } }