I have a situation where I have to generate a lot of HTML and then return it as a string JSONP style. So the eventual HTTP response will actually be javascript text like this:
myglobaljavascriptcallbackfunction('
<HTML here>');
Since the HTML is complex, the only sane way to construct it is with a JSP. So what I would like to do is take the HTML output of the JSP and pipe it to a servlet which can then wrap the HTML with the necessary javascript.
Below is my best guess so far. No luck - the HTTP response from the Servlet is myglobaljavascriptcallbackfunction('');
without any of the JSP's HTML.
JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:forward page="/MyServlet" />
<div>
<span>Imagine some really complicated stuff here</span>
<div>
Servlet
protected void doGet(...) {
String pre = "myglobaljavascriptcallbackfunction('";
String post = "');";
OutputStream out = response.getOutputStream();
out.write(pre.getBytes());
// transfer request to response
InputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) > 0) {
out.write(buf, 0, count);
// TODO: escape single quote chars
}
out.write(post.getBytes());
}