Rendering JSP into dummy response to obtain HTML
Is it possible to render a JSP into a mock response?
A project I'm working on requires me to render a series of small JSPs that are currently being included into a parent JSP. I would like to be able to render each individually, and then store the resulting String from the response for later use. I don't want to actually affect the real httpservletresponse at this time, just obtain the String from the JSP, store it, and throw away the response.
Is there an api besides requestdispatcher.forward() and .include() that can be used?
I tried creating a simple implementation of httpservletresponse, but it actually crashed my tomcat when I tried to pass that to the requestdispatcher.
I'm thinking about trying the mocks provided by spring, but I'm not crazy about the idea of having 'test' code within a running application ( I guess you don't have to look at it that way though).
Someone please tell me if I'm trying to do something impossible here:)
# 3
Couple of ways you could do it
1 - From a standalone application.
Use the java.net classes and make a URLConnection to Tomcat, and access the JSP page. Save the response to file. This would be pretty much the equivalent of loading the page in your browser, viewing source and saving that.
2 - Internally, use a HttpServletResponseWrapper.
Write a wrapper that overrides the getOutputStream/getWriter methods and returns an OutputStream/Writer that will send the output to a file, rather than writing to the servlet output stream.
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets8.html
# 5
Directly extend HttpServletResponseWrapper. Thats what its there for!
Actually, heres one I had floating around somewhere. I think I stole the code from somewhere around the net myself :-)
package evnafets.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* @author evnafets
*
*/
RedirectServletStream out;
PrintWriter writer;
public RedirectingServletResponse(HttpServletResponse response, OutputStream out) {
super(response);
this.out = new RedirectServletStream(out);
}
/* (non-Javadoc)
* @see javax.servlet.ServletResponse#flushBuffer()
*/
public void flushBuffer() throws IOException {
if(writer != null){
writer.flush();
}
out.flush();
}
/* (non-Javadoc)
* @see javax.servlet.ServletResponse#getOutputStream()
*/
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
/* (non-Javadoc)
* @see javax.servlet.ServletResponse#getWriter()
*/
public PrintWriter getWriter() throws IOException {
if (writer == null){
writer = new PrintWriter(out);
}
return writer;
}
private class RedirectServletStream extends ServletOutputStream {
OutputStream out;
RedirectServletStream(OutputStream out) {
this.out = out;
}
public void write(int param) throws java.io.IOException {
out.write(param);
}
public void flush() throws IOException {
out.flush();
}
}
}
Basically to use in your servlet/filter/whatever you are running
FileOutputStream out = new FileOutputStream("myFile.txt");
RedirectingServletResponse redirector = new RedirectingServletResponse(response, out);
requestDispatcher.forward(request, redirector );
Or something like that anyway :-)