Search code examples
emailjspjakarta-mailportal

How to get the result of a JSP in a java method?


I am working on a restaurant web portal which will send periodic emails to the registered users such as changes in menu, discounts and offers, birthday wishes etc.

Here is how I am doing it-

  1. I create a JSP page with the HTML formatting. This page accepts certain parameters that will customize the page.
  2. The MailDaemon scans the db and upon a certain trigger will make a GET request to the JSP.
  3. The returned JSP represents the final mail content which is then sent using the JavaMail API.

My questions are these -

  1. Do I really need to make a GET request? Or can I obtain the JSP result directly in my Daemon class?
  2. Does my approach have any drawbacks? Any better solutions (architecturally speaking)
  3. Any other templating solutions for the job?

Solution

    1. It's possible to buffer a response from your JSP using javax.servlet.RequestDispatcher#include by wrapping original response in javax.servlet.ServletResponseWrapper (in case your MailDaemon is a servlet)
    2. Your approach is OK. Though it'll be easier to leverage another templating engine.
    3. Apache Velocity or FreeMarker. These two are not tied to web, so you can easily use them without servlet container.