简体   繁体   English

提取JSP页面内容

[英]Extracting JSP page content

I am working on a Mail API module where i have to develop a generic functionality of sending Mails with respect to various functionality in the Appliication. 我正在开发一个Mail API模块,其中我必须针对应用程序中的各种功能开发一种发送邮件的通用功能。

There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template. 有一种建议的功能,其中模块将公开一种方法,该方法连同一些必需的参数将占用JSP模板的全部。 It expects that this will extract the content of the JSP which will be a well formated mail template and send mail. 它期望这将提取JSP的内容,该内容将是格式正确的邮件模板并发送邮件。

Is there any way in JAVA where i can extract the content (HTML) from this JSP page so that i can use that HTML content in to the Mail. JAVA中有什么方法可以从此JSP页面提取内容(HTML),以便可以将HTML内容用于Mail。

Thanks in advance 提前致谢

You have two paths to go, with the first one being a little shorter: 您有两条路要走,第一条路要短一些:

  • use new URL("http://site.com/url/to/page.jsp").openConnection() , get the InputStream and read the contents - this will be as if your server sends a request to itself and gets the result 使用new URL("http://site.com/url/to/page.jsp").openConnection() ,获取InputStream并读取内容-就像您的服务器向自身发送请求并获取结果

  • use a Filter and a HttpServletResponseWrapper , and return a custom Writer / OutputStream . 使用FilterHttpServletResponseWrapper并返回自定义Writer / OutputStream Each time something is written to the writer / stream, delegate it to the original object, and also write it somewhere where you can read it from later. 每次将某些内容写入writer / stream时,将其委托给原始对象,并将其写入可以在以后读取的位置。 This explanation is not sufficient, because this is less likely what you need, but if you are willing to take this path, tell me. 这种解释是不够的,因为您所需的可能性较小,但是如果您愿意采用这种方式,请告诉我。

That's, however, not the way this is usually done. 但这不是通常的方式。 You'd better use some templating technology like Freemaker or Velocity for your email templates. 您最好为电子邮件模板使用一些模板技术,例如FreemakerVelocity

It sounds like you're trying to use JSPs as a templating engine for your email, which is something it wasn't intended to do. 听起来您好像正在尝试将JSP用作电子邮件的模板引擎,这并不是本来打算做的。 There are other technologies out there better suited for what you want, like Velocity and Freemarker. 还有其他一些技术更适合您的需求,例如Velocity和Freemarker。

However, if you're dead-set on using JSP, you have two options : 1) You can use the method described by Bozho to, essentially, connect to your own site and have it generate the content for you 2) You can write the JSP, compile it at compile time, and include the generated servlet file in your email generator and mock the inputs to the Servlet API that the generated JSP servlet will be expecting to extract content from your compiled JSP. 但是,如果您对使用JSP有所犹豫,则有两个选择:1)您可以使用Bozho所述的方法来连接到您自己的站点,并为您生成内容2)您可以编写JSP,在编译时对其进行编译,并将生成的servlet文件包括在您的电子邮件生成器中,并模拟Servlet API的输入,生成的JSP servlet希望从该API中提取已编译的JSP的内容。

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

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