Reputation: 21
The basic servlet jsp setup I'm familiar with ....
RequestDispatcher dispatcher = request.getRequestDispatcher(resourceA.jsp);
dispatcher.forward(request, response);
The problem is, in addition to sending the reply back to the browser (resourceA.jsp), I need to create a second HTML output from resourceB.jsp, witch in turn will be emailed or used to create pdf document.
This is not done every time, just in specific conditions and by calling
dispatcher.forward(request, response);
the second time I get and IllegalArgumentException.
I just need a way in generating HTML from jsp (basically replacing the ${par} values) and returning the html as String, no status code or headers, just html and A have nothing to do with B, so Filters will not work.
------ replay to first Answer ----- comment to small --- no formatting
I can't see why this is so difficaled....
all the magnesium's is there to merge data with HTML content in the
${par}
${par['key']}
<c:if test="${patBoolean}" >
<c:forEach var="pvar" varStatus="s_pvar" items="${parList}">
I even created a wrapper to mask getOutputStream() and getWriter(), creating my own PrintWriter
public class ResponseWrapper extends HttpServletResponseWrapper{
private PrintWriter printWriter;
public ResponseWrapper(HttpServletResponse response){
super(response);
}
public ServletOutputStream getOutputStream() throws java.io.IOException{....}
public PrintWriter getWriter() throws java.io.IOException {....}
}
in getting the result for the resourceB.jsp, but the problem is dispatcher.forward(request, response) sets isCommitted some ware in the original HttpServletResponse.
Upvotes: 2
Views: 5794
Reputation: 1008
It is very possible.
Yes maybe it's not the right use case maybe just a workaround but it is easy. Who cares.
Once you have a HttpServletRequest
instance, you can easily construct a mock HttpServletResponse
to redirect jsp output to a String.
You need a StringWriter
In order to get the JSP output you need a StringWriter
.
StringWriter sw=new StringWriter();
Wrap it
JSP parser or something need a PrintWriter
.
PrintWriter pw=new PrintWriter(sw);
Construct a mock response
You can't reuse an existing HttpServletResponse
easily, so build your own.
HttpServletResponse res=new HttpServletResponse() {
@Override
public PrintWriter getWriter() throws IOException {
// return your own writer!
return pw;
}
}
You have to implement other methods too but just use your IDE's quick fix and leave them default.
Generate it
something.jsp
should be an existing one.
request.getRequestDispatcher("/something.jsp").forward(request,res);
Get the output
This would print the JSP output to server console.
System.out.printf("{%s}",sw.toString());
Continue with the real response
Using servlets you should continue to deal with the real HttpServletResponse
just like you haven't generate the JSP before. While in Spring there's no real Response
to care about.
Till now I still cannot mock a Request as well as Response that works. But with a mock Response it's already enough to solve this question.
Upvotes: 1
Reputation: 309028
JSP is not the thing to do here. Have a separate process that fills out a Velocity template (for email) or generates a PDF using XSL-FO or iText and leave JSP out of it. It's a completely separate use case from sending back an HTTP response.
Upvotes: 0