Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a jsp page from code and get the rendered html output as a string

What's the easiest way to make a JSP page render, then get the rendered html content as a string?

like image 212
user802232 Avatar asked Nov 15 '11 10:11

user802232


2 Answers

There is this tutorial, which explains every step with code:

http://valotas.com/get-output-of-jsp-or-servlet-response/

Doing it this way has advantages when the JSP is not accessible by URL directly.

like image 84
barfuin Avatar answered Nov 14 '22 23:11

barfuin


You should provide your own wrapper for the Writer of HttpServletResponse (via HttpServletResponseWrapper in a Filter), and each time you write to that writer, also store in a StringBuilder.

That's just a sketch of the code, there is a sufficient number of examples, but the main steps are:

  • create a filter
  • wrap PrintWriter to make it store each write in a builder
  • extend HttpServletResponseWrapper and make it return the writer wrapper
  • create chain.doFilter(request, new HttpServletResponseWrapper(response))
like image 22
Bozho Avatar answered Nov 15 '22 00:11

Bozho