views:

29

answers:

2

Hi All,

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

+2  A: 

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

  • use a Filter and a HttpServletResponseWrapper, and return a custom 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. 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.

Bozho
First approach seems reasonable to me but for second may be i am not able to understand it,since the mail templates will be with in the application and they will not change a lot so i am not able to see the relation of this with Filters or Wrappers
+1  A: 

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.

Alex Marshall
@Alex this is just a proposal and not yet finalised.But since we are using mainly using JSP for all presentation so may be they have some prefrence for JSP.bu from my side i want to go for best one if they allow me 100% freedom :)