[英]Render a jsp page from code and get the rendered html output as a string
制作JSP页面渲染的最简单方法是什么,然后将渲染的html内容作为字符串?
There is this tutorial, which explains every step with code: 本教程使用代码解释每个步骤:
http://valotas.com/get-output-of-jsp-or-servlet-response/ 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. 当URL无法直接通过URL访问时,以这种方式执行此操作具有优势。
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
. 您应该为HttpServletResponse
的Writer
提供自己的包装器(通过Filter
HttpServletResponseWrapper
),每次写入该编写Filter
时,也存储在StringBuilder
。
That's just a sketch of the code, there is a sufficient number of examples, but the main steps are: 这只是代码的草图,有足够数量的例子,但主要步骤是:
PrintWriter
to make it store each write in a builder 将PrintWriter
包装起来,使其在构建器中存储每个写入 HttpServletResponseWrapper
and make it return the writer wrapper 扩展HttpServletResponseWrapper
并使其返回编写器包装器 chain.doFilter(request, new HttpServletResponseWrapper(response))
create chain.doFilter(request, new HttpServletResponseWrapper(response))
使用java.net.URL
和java.net.URLConnection
类方法或JSTL <c:import/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.