Capture generated server-side dynamic content

Is there a way by which I can capture the generated dynamic content on the server side and get this file or string object with this servlet.

We can generate dynamic content using JSP, but we do not have access to the generated dynamic content on the server side. Once we do, the forwarding container creates dynamic content and sends it back.

I need access to the generated dynamic content on the server side.

Any help would be appreciated.

+13
java jsp servlets
Dec 26 '09 at 11:08
source share
4 answers

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 { // NOOP. } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Set character encoding for better world domination. response.setCharacterEncoding("UTF-8"); // Create copy writer. HttpServletResponse httpResponse = (HttpServletResponse) response; CopyWriter copyWriter = new CopyWriter(new OutputStreamWriter( httpResponse.getOutputStream(), httpResponse.getCharacterEncoding())); // Filter request with response which is wrapped with new writer. chain.doFilter(request, wrapResponse(httpResponse, copyWriter)); // Store the copy writer afterwards in session so that it available in next request. HttpServletRequest httpRequest = (HttpServletRequest) request; httpRequest.getSession().setAttribute("copyWriter", copyWriter); } public void destroy() { // NOOP. } private static HttpServletResponse wrapResponse (final HttpServletResponse response, final PrintWriter writer) { return new HttpServletResponseWrapper(response) { public PrintWriter getWriter() throws IOException { return writer; } }; } } class CopyWriter extends PrintWriter { StringBuilder copy = new StringBuilder(); public CopyWriter(Writer out) { super(out); } public void write(int c) { copy.append((char) c); // It is actually a char, not an int. super.write(c); super.flush(); } public void write(char[] chars) { copy.append(chars); super.write(chars); super.flush(); } public void write(char[] chars, int offset, int length) { copy.append(chars, offset, length); super.write(chars, offset, length); super.flush(); } public void write(String string) { copy.append(string); super.write(string); super.flush(); } public void write(String string, int offset, int length) { copy.append(string, offset, length); super.write(string, offset, length); super.flush(); } public String getCopy() { return copy.toString(); } } 

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.

+15
Dec 26 '09 at 15:06
source share
— -

As far as I understand your goal, you want to save the generated content. Although I cannot think of any reason for this, it is possible.

You can use Filter and create a wrapper for your HttpServletResponse , override some write methods and capture all the content that will be displayed in the browser.

Edit: BalusC's answer is the exact extension you need, so I won't go into details.

+2
Dec 26 '09 at 12:55
source share

You must have access to the appropriate servlets on the server side, or you can write your servlet.

Here 's a little tutorial on using servlets.

0
Dec 26 '09 at 11:16
source share

It is important to note that JSPs are compiled into servlets through the JSP compiler (which can generate Java code in half), so all you can do in JSP is what you can do with servlets.

One-sided note on this subject: you need to know MVC , generally speaking, servlets act as controllers, JSPs in the form of a View and any data structures (java beans, etc.) act as a Model.

For your problem, you must generate content / data in the servlet - this is your dynamic content represented by objects, perform any processing that you need to perform, set this data so that the JSP can access it (in the request attribute ), access and format its for customer in jsp.

To find out, you need to check Servlet History: An Instant Tutorial , a Web-based Java ™ EE 5 Tutorial .

0
Dec 26 '09 at 12:02
source share



All Articles