Hi,
I have a web app which I’m running on Jetty (for my CI server) and Tomcat (in Eclipse and for live deployment).
I have email templates which are rendered by Apache Tiles, which is backed by JSP pages.
I would like to send an email from a background thread in my web-app (i.e. not a request thread).
I searched around for how to render JSP pages outside of a request thread, and found e.g. [1], [2], which suggest creating a mock Request/Response, fetching the RequestDispatcher from the ServletContext and calling dispatcher.forward(request,response).
I tried this, and it works fine in Tomcat. Unfortunately it doesn’t work in Jetty.
The problem is that the org.mortbay.jetty.servlet.Dispatcher class has this line at the start of the “forward” method:
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
protected
void forward(ServletRequest request, ServletResponse response,
int dispatch)
throws ServletException, IOException
{
Request base_request=(request
instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
.. i.e. the Jetty Dispatcher will only dispatch when the ThreadLocal Jetty Request is available in HttpConnection, or when passed an instance of
org.mortbay.jetty.Request.
This won’t work for me, because I’m in a background thread, so the ThreadLocal is null, and I don’t think I should tie my application to Jetty’s API by creating a Jetty Request.
Does anyone have any suggestions about what I can do instead?
There are two angles of attack which occur to me:
1. Isn’t this a bug in the Dispatcher? Why does it need to insist that the request is a
org.mortbay.jetty.Request? Why can it not forward a generic ServletRequest?
2. Is there a way to render JSP files inside a Jetty ServletContext without using Dispatcher.forward? (Ideally this would work on Tomcat as well, but I don’t mind too much if not.)
3. Should I detect whether I’m running on Jetty and create a
org.mortbay.jetty.Request object? Can I just create a blank instance of that class? How should I detect whether I’m running on Jetty?
Thanks,
Rich
[1]
http://stackoverflow.com/questions/1075827/execute-jsp-directly-from-java
[2]
http://technologicaloddity.com/2011/10/04/render-and-capture-the-output-of-a-jsp-as-a-string/