[jetty-users] render JSP from a background thread in a web app

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[jetty-users] render JSP from a background thread in a web app

Richard Bradley



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?









[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/


Richard Bradley
Tel : 020 7485 7500 ext 230 | Fax : 020 7485 7575 

softw i re        
Sunday Times Best Small Companies – UK top 20
Web : www.softwire.com | Addr : 325 Highgate Studios, 53-79 Highgate Road, London NW5 1TL 

Softwire Technology Limited. Registered in England no. 3824658. Registered Office : 13 Station Road, London N3 2SB

jetty-users mailing list
[hidden email]