Reputation: 6004
Is there any way with which I can capture generated dynamic content on the server side and get that file or string object of the same to the servlet.
We can generate dynamic content with JSPs, but we dont have access to the generated dynamic content at the server side. As soon as we do the forward container generates the dynamic content and sends it to response.
I need access to generated dynamic content on the server side.
Any help will be appreciated.
Upvotes: 13
Views: 10011
Reputation: 1109122
If the request is idempotent (such as GET
requests are), then just use java.net.URL
to get an InputStream of the JSP output. E.g.
InputStream input = new URL("http://localhost/context/page.jsp").openStream();
If the request is not idempotent (such as POST
requests are), then you need to create a Filter
which wraps the ServletResponse
with a custom implementation of the PrintWriter
with the five write()
methods been overridden wherein you copy output into some buffer/builder which you store in the session or a temporary folder at local disk file system so that it can be accessed afterwards in the subsequent requests. E.g.
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's 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's already too late to do something with it) by just accessing the CopyWriter
in the session:
CopyWriter copyWriter = (CopyWriter) request.getSession().getAttribute("copyWriter");
String outputOfPreviousRequest = copyWriter.getCopy();
Note that you should map this filter on an url-pattern
covering the JSP pages of interest and thus not on /*
or so, otherwise it would run on static files (css, js, images, etc) which are included in the same JSP as well.
Also note that multiple requests inside the same session would override each other, it's up to you to distinguish between those requests by using a proper url-pattern
or another way of storing it in session, e.g. in flavor of a Map<URL, CopyWriter>
or so.
Hope this helps.
Upvotes: 16
Reputation: 597224
As far as I understand your aim, you want to store the generated content. While I can't think of any reason to do this, it is possible.
You can use a Filter
and create a wrapper of your HttpServletResponse
, override some write
methods and capture all the content that will be outputted to the browser.
Edit: BalusC's answer is the exact extension you need, so I won't go into the details.
Upvotes: 2
Reputation: 4690
Important thing to note is that JSPs are compiled to servlets via JSP compiler (that might generate java code half way trough), so anything you can do in a JSP you can do with servlets.
One side note on the subject: you should be aware of MVC, generally speaking servlets act as Controllers, JSPs as View, and any data structures (java beans and such) act as the Model.
For your problem you should generate the content/data in the servlet -- this is your dynamic content represented by objects, do whatever processing you need to do, set this data so that JSPs can access it (in request attribute for example), access and format it for the client in the JSP.
To get familiar you should check out Story of a Servlet: An Instant Tutorial, The Java™ EE 5 Tutorial on Web tier.
Upvotes: 0
Reputation: 24791
You need to have access to appropriate servlets at the server side or you can write your servlet.
Here is a small tutorial on using servlets.
Upvotes: 0