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.
There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template. It expects that this will extract the content of the JSP which will be a well formated mail template and send mail.
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.
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 theInputStream
and read the contents - this will be as if your server sends a request to itself and gets the resultuse a
Filter
and aHttpServletResponseWrapper
, and return a customWriter
/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. 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.
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. There are other technologies out there better suited for what you want, like Velocity and 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.
精彩评论