Saturday, November 23, 2019

Java.Lang.Illegalstateexception: Getoutputstream() Has Already Been Called For This Response

This fault comes when y'all telephone vociferation upwards include() or forward() method afterward calling  the getOutputStream() from ServletResponse object as well as writing into it.  This fault is like to java.lang.IllegalStateException: getWriter() has already been called for this answer error, which nosotros accept seen inwards 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)

as well as 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 y'all expect at the stack describe it points to HelloServlet class, which is our Servlet, the business 25 of HelloServlet seems to live on causing the problem.

You tin dismiss run into that nosotros are calling the include() method afterward calling the getOutputStream() on Servlet object. Just comment out the telephone vociferation upwards to getOutputStream() and everything volition live on Ok.  To acquire to a greater extent than close Servlet as well as JSP,  read Head First Servlet as well as JSP, ane of the best books from concluding 10 years for learning JSP as well as Servlet.



Here is the consummate Java Servlet for your testing, y'all tin dismiss 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 fault inwards Servlet. You accept learned what crusade this fault as well as how to laid upwards this. In short, this fault comes when y'all telephone vociferation upwards the include() or forward() method afterward committing the answer or past times calling the getOutputStream() on the answer object. 

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets as well as JDBC for Beginners: Build a Database App

No comments:

Post a Comment