How to read a JSP file from WEB-INF into a variable for subsequent "printing",
I would like to know how the file put in WEB-INF can be read directly into a variable. My intent is to load (and render the page) in var and then save it to a file and furthermore use the same html result that will be rendered (eg included) in another JSP directly.
Is it possible?
You can use RequestDispatcher.include () method and buffered response. The latter you need to write by extending HttpServletResponseWrapper and overriding the getWriter()
/ methods getOutputStream()
to return an internal buffer (based on StringWriter
, for example). In your servlet, do something like:
MyBufferedResponseWrapper buffer = new MyBufferedResponseWrapper(response); // wrap real response
getServletContext().getRequestDispatcher("path_to_your_jsp").include(request, buffer);
String output = buffer.getOutput();
You can hide jsp pages under WEB-INF which can be <jsp: included>.
Are you expecting the benefits of caching?