August 2, 2009

Servlet: getReader() has already been called for this request

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getReader() has already been called for this request
 org.apache.catalina.connector.Request.getInputStream(Request.java:934)
 org.apache.catalina.connector.RequestFacade.getInputStream(RequestFacade.java:328)
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:34)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The input from the request body can be either done with the BufferedReader object procured from the request.getReader() method or with the ServletInputStream object procured from the request.getInputStream() method, but not both, in the Servlet.

If the getReader() is already called on the request object, the call to getInputStream() on the same request object will throw an IllegelStateException and vice versa.

No comments:

Post a Comment