Is it possible to publish the output of a JSP page to a static HTML file?

Suresh Payankannur

1. You can access the JSP page programatically using a URLConnection, by simulating a browser connection.

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.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact