简体   繁体   English

从代码渲染jsp页面并将渲染的html输出作为字符串

[英]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 . 您应该为HttpServletResponseWriter提供自己的包装器(通过Filter HttpServletResponseWrapper ),每次写入该编写Filter时,也存储在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 PrintWriter包装起来,使其在构建器中存储每个写入
  • extend HttpServletResponseWrapper and make it return the writer wrapper 扩展HttpServletResponseWrapper并使其返回编写器包装器
  • create chain.doFilter(request, new HttpServletResponseWrapper(response)) create chain.doFilter(request, new HttpServletResponseWrapper(response))

使用java.net.URLjava.net.URLConnection类方法或JSTL <c:import/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM