I'm struggling with the following situation:
In our current web application running on Tomcat 7.0.64, we manage to include a JSP page via Java with the help of an own class CharArrayWriterResponse implementing HttpServletResponseWrapper
.
The reason for doing so is that we wrap the resulting HTML into JSON needed for an AJAX Response.
Dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Code example:
// somewhere in servlet doPost()/doGet()
try (PrintWriter out = response.getWriter()) {
out.println(getJspAsJson(request, response));
}
private static String getJspAsJson(HttpServletRequest request, HttpServletResponse response) {
String html = getHtmlByJSP(request, response, "WEB-INF/path/to/existing.jsp");
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
return "{\"results\":" + gson.toJson(html) + "}";
}
public static String getHtmlByJSP(HttpServletRequest request, HttpServletResponse response, String jsp) {
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(response);
request.getRequestDispatcher(jsp).include(request, customResponse);
return customResponse.getOutput();
}
public class CharArrayWriterResponse extends HttpServletResponseWrapper {
private final CharArrayWriter charArray = new CharArrayWriter();
public CharArrayWriterResponse(HttpServletResponse response) {
super(response);
}
@Override
public PrintWriter getWriter() throws IOException {
// this is called ONLY in tomcat
return new PrintWriter(charArray);
}
public String getOutput() {
return charArray.toString();
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
// this is called ONLY in WebLogic
return null; // don't know how to handle it
}
}
Hint: I didn't consider exception handling in above code samples.
I have to migrate this application to WebLogic (12.2.1) but this solution is not working anymore.
What I found out so far:
In Tomcat after the call to request.getRequestDispatcher(jsp).include()
of the example above getWriter()
of my CharArrayWriterResponse
class is called.
In WebLogic getWriter()
is not called anymore and that's the reason why it doesn't work anymore.
After some debugging, I found out that in WebLogic instead of getWriter()
only getOutputStream()
is called if I override it.
getWriter()
is not called once on Weblogic so there have to be differences in the underlying implementation of Tomcat and WebLogic.
Problem is that with getOutputStream()
I see no possibility to get the response of the include()
call in a separate stream or something else and to convert it to String for usage to build the final JSON containing the HTML.
Has someone solved this problem already and can provide a working solution for including a JSP in a programmatic way in combination with WebLogic?
Does anyone know another solution to achieve my goal?
Thanks for suggestions.
Solution
See working example here
Hint
A difference I found out between Tomcat and new Weblogic solution:
With latter one it's not possible to include JSPF's directly anymore wheras with Tomcat getWriter()
it is.
Solution is wrapping the JSPF inside a JSP file.