Is it possible to publish the output of a JSP page to a static HTML file?
Created May 4, 2012
Suresh Payankannur 1. You can access the JSP page programatically using a URLConnection, by simulating a browser connection.
2. A much more involved approach is to use a wrapper around ServletResponse, similar to the class
By extending the wrapper concept to provide your own ServletOutputStream, you could simultaneously provide the JSP output to both the client (browser) and to your own stream.
URL url = new URL("http://www.jguru.com/"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); OutputStream out = new FileOutputStream("mypage.html"); int c = -1; while((c = in.read()) != -1) { out.write(c); }
2. A much more involved approach is to use a wrapper around ServletResponse, similar to the class
javax.servlet.ServletResponseWrapper
in the Servlet 2.3 API (Tomcat 4.0). Then you can override the getOutputStream()
and getWriter()
methods to provide your own stream.
MyServletResponseWrapper wrapper = new MyServletResponseWrapper(servletResponse); RequestDispatcher disp = getServletContext().getRequestDispatcher("/pages/mypage.jsp"); disp.forward(servletRequest, wrapper);
By extending the wrapper concept to provide your own ServletOutputStream, you could simultaneously provide the JSP output to both the client (browser) and to your own stream.