If the request is idempotent (e.g. GET request are)), simply use java.net.URL to get the InputStream JSP output. For example.
InputStream input = new URL("http://localhost/context/page.jsp").openStream();
If the request is not idempotent (for example, POST requests), you need to create a Filter that wraps ServletResponse using a special PrintWriter implementation with five write() methods that were canceled when you copy the output to some buffer / builder that you store in the session or temporary folder in the local file system on the disk, so that later it can be obtained in subsequent requests. For example.
package mypackage; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; public class CopyResponseFilter implements Filter { public void init(FilterConfig config) throws ServletException {
You can access the final output in any servlet of the subsequent request (note that you cannot access it in any servlet of the current request, because it is too late to do anything with it) just access CopyWriter in the session:
CopyWriter copyWriter = (CopyWriter) request.getSession().getAttribute("copyWriter"); String outputOfPreviousRequest = copyWriter.getCopy();
Please note that you must map this filter to a url-pattern covering JSP pages of interest, and therefore not on /* or so, otherwise it will work on static files (css, js, images, etc.), which are included in the same JSP.
Also note that several requests within the same session will override each other, you need to distinguish between these requests using the correct url-pattern or another way to save it in the session, for example. in the flavor of Map<URL, CopyWriter> or so.
Hope this helps.
BalusC Dec 26 '09 at 15:06 2009-12-26 15:06
source share