java.lang.IllegalStateException: getOutputStream() has already been called for this response

This mistake comes when you lot telephone phone include() or forward() method afterwards calling  the getOutputStream() from ServletResponse object in addition to writing into it.  This mistake is like to java.lang.IllegalStateException: getWriter() has already been called for this answer error, which nosotros bring seen inward the earlier article.

This is the exception:
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
HelloServlet.doGet(HelloServlet.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

in addition to hither is the root cause :



java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:611)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:180)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
org.apache.jsp.hello_jsp._jspService(hello_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
HelloServlet.doGet(HelloServlet.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


If you lot hold back at the stack describe it points to HelloServlet class, which is our Servlet, the line of piece of job 25 of HelloServlet seems to travel causing the problem.

You tin run across that nosotros are calling the include() method afterwards calling the getOutputStream() on Servlet object. Just comment out the telephone phone to getOutputStream() and everything volition travel Ok.  To larn to a greater extent than almost Servlet in addition to JSP,  read Head First Servlet in addition to JSP, ane of the best books from concluding 10 years for learning JSP in addition to Servlet.



Here is the consummate Java Servlet for your testing, you lot tin run this Servlet into Tomcat correct from your Eclipse IDE.
import java.io.IOException;  import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  public class HelloServlet extends HttpServlet {      public void doGet(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         String userAgent = req.getHeader("user-agent");         String clientBrowser = "Not known!";         if (userAgent != null) {             clientBrowser = userAgent;         }         req.setAttribute("client.browser", clientBrowser);         //req.getRequestDispatcher("/hello.jsp").forward(req, resp);          resp.getOutputStream().println("This is written from Servlet");         req.getRequestDispatcher("/hello.jsp").include(req, resp);     }  }

That's all about java.lang.IllegalStateException: getOutputStream() has already been called for this response mistake inward Servlet. You bring learned what crusade this mistake in addition to how to fix this. In short, this mistake comes when you lot telephone phone the include() or forward() method afterwards committing the answer or past times calling the getOutputStream() on the answer object. 

Subscribe to receive free email updates:

0 Response to "java.lang.IllegalStateException: getOutputStream() has already been called for this response"

Posting Komentar