0

First of all, I wanna apologize for not so clear description of the question. Let's consider we have some working mechanism for e-mail sending, and we also have some e-mail templates mechanism based on JSP processing engine. Let's say, these templates may be directly accessed like http://localhost/app/templates/template1.jsp?k1=v1&k2=v2 and always return an HTML that can be inserted into an e-mail for a certain user in the back-end code. Simply saying, the short-hand code might look like:

Map<String, String> parameters = new HashMap<String, String>() {{ ..puts.. }};
String message = readJsp("/templates/template1.jsp", parameters); // servlet-context?
sendEMail(recipient, subject, message)

Currently I found two solutions for implementing the readJsp method. The first approach is to make a HTTP request from the servlet to the web-server where both servlet and JSP-template are put at. Well, I'm not a fan of this...

The second and much better way that really works is getting a request dispatcher from the servlet context and invoking its include() method with the response wrapper (this work around is described here). This method works perfect, but I don't know how I can use this approach if I cannot get a HttpServletRequest and HttpServletResponse instances (let's consider my sendEMail() method can be invoked both from servlet service method [it's easy to get the HttpServletRequest/Response instances here] and a thread that runs as a part of the web application [where HttpServletRequest/Response instance are never known and even cannot be passed to the thread]). I can suggest that I can create the HttpServletRequest/HttpServletResponse instances myself in a certain way, but I don't know how and whether it will be ok for that readJsp() method based on the second approach.

So, the question is basically about, perhaps, the 3rd (and even better) way of getting the JSP content from a thread that does not belong to a servlet but runs in the same web application. Or is it possible to create valid HttpServletRequest/HttpServletResponse instances from scratch to make the second approach work out of servlet service() method?

Any ideas? Thanks in advance.

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
  • possible duplicate of [Capture generated dynamic content at server side](http://stackoverflow.com/questions/1963158/capture-generated-dynamic-content-at-server-side) – BalusC Apr 13 '11 at 13:46
  • Also [Spring MVC - AJAX-JSON Response to contain rendered JSP view](http://stackoverflow.com/questions/5489504/spring-mvc-ajax-json-response-to-contain-rendered-jsp-view/5490775#5490775) – sourcedelica Apr 13 '11 at 14:02
  • @ericacm, consider I have neither original request nor original response objects because I have a thread that have no way to take the request and request from. And... the JSP outout is not transferred to a client. The JSP output should be put into the e-mail body sent by the sender thread. – Lyubomyr Shaydariv Apr 13 '11 at 14:10

1 Answers1

2

You could try using Spring's MockHttpServletRequest/Response but from what I have heard, Tomcat's ServletDispatcher.include() does not work with those.

I would recommend not using JSP in the case where you don't have access to a request/response pair. Instead use Velocity or Freemarker to template your emails.

sourcedelica
  • 23,940
  • 7
  • 66
  • 74
  • Actually I have chosen the JSP because it can be pretty integrated into the i18n mechanism we implemented in our project (and most JSP pages are already i18n-ed using that). I have a bit experience in FreeMarker, and no in Velocity. But I'll take a look. Thanks. – Lyubomyr Shaydariv Apr 13 '11 at 15:21
  • The Velocity engine has been involved, though I still can't get some effects I reached using the JSP engine. Thanks. – Lyubomyr Shaydariv Apr 14 '11 at 09:00