#41 Unable to include a servlet with the RequestDispatcher

open
nobody
None
5
2010-01-10
2010-01-10
Emmanuel Bourg
No

There is a issue with RequestDispatcher.include() in Winstone 0.9.10 which prevents the inclusion of another servlet. It looks like the included servlet overwrites the buffer of the calling servlet, and if the calling servlet first flushes the output before including the other servlet, an IllegalStateException is thrown with the message : "Called RequestDispatcher.forward() on committed response". This issue also breaks the usage of the jsp:include tag.

Here is an example :

Servlet A :

response.getWriter().println("INCLUDED CONTENT");

Servlet B :

response.setContentType("text/plain");
response.getWriter().println("PRE INCLUDE");

request.getRequestDispatcher("/path/to/servletA").include(request, response);
response.getWriter().println("POST INCLUDE");
response.getWriter().flush();

Calling the servlet B displays only "INCLUDED CONTENT", the pre/pst message aren't in the response body. And flushing the output before calling RequestDispatcher.include() throws an IllegalStateException :

java.lang.IllegalStateException: Called RequestDispatcher.forward() on committed response
at winstone.RequestDispatcher.forward(RequestDispatcher.java:263)
at winstone.invoker.InvokerServlet.doGet(InvokerServlet.java:134)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:45)
at winstone.ServletConfiguration.execute(ServletConfiguration.java:249)
at winstone.RequestDispatcher.include(RequestDispatcher.java:200)
at ServletB.doGet(ServletA.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:45)
at winstone.ServletConfiguration.execute(ServletConfiguration.java:249)
at winstone.RequestDispatcher.forward(RequestDispatcher.java:335)
at winstone.invoker.InvokerServlet.doGet(InvokerServlet.java:134)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:45)
at winstone.ServletConfiguration.execute(ServletConfiguration.java:249)
at winstone.RequestDispatcher.forward(RequestDispatcher.java:335)
at winstone.RequestHandlerThread.processRequest(RequestHandlerThread.java:244)
at winstone.RequestHandlerThread.run(RequestHandlerThread.java:150)
at java.lang.Thread.run(Thread.java:619)

Discussion