Extract the jsp page from the code and get the displayed html output as a string

What is the easiest way to render a JSP page and then get the html display content as a string?

+6
source share
3 answers

Use class methods java.net.URL and java.net.URLConnection or JSTL <c:import/>

+1
source

Here is this tutorial that explains each step with the code:

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

This has advantages when the JSP is not reachable at the URL directly.

+7
source

You must provide your own wrapper for the Writer HttpServletResponse (via the HttpServletResponseWrapper in Filter ), and each time you write to this writer, also save it in StringBuilder .

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

  • create filter
  • wrap PrintWriter so that it saves every entry in the builder
  • expand HttpServletResponseWrapper and make it return author wrapper
  • create chain.doFilter(request, new HttpServletResponseWrapper(response))
+3
source

Source: https://habr.com/ru/post/901499/


All Articles