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

后端 未结 3 1243
独厮守ぢ
独厮守ぢ 2020-12-22 09:41

I am new to jsp,when I try to invoke a jsp page by some parameters named cId and passWord,I getting this error,The code I have been trying is given below,I have already gone

相关标签:
3条回答
  • 2020-12-22 09:59

    This is a JSP with scriplet which is converted into a Servlet file. You dont need to call explicitly the response object. If you need to see how a compiled JSP looks like when its deployed , search (Google) how to look for the compiled class(Servlet generated out of JSP) on the server. Since you have already called the method on the response a second invocation is Illegal on the response object

    0 讨论(0)
  • 2020-12-22 10:12

    You shouldn't try and do this inside a JSP. The JSP will already have obtained an output stream to write it's output. You need to use a servlet to return your XML.

    When you call response.getOutputStream, it is conflicting with the fact that the JSP (which will be compiled into a servlet) already obtained an output stream. This is why it is resulting in the IllegalStateException.

    0 讨论(0)
  • 2020-12-22 10:20
    • If you check the documentation of getOutputStream() method: It mentions

    Throws:IllegalStateException - if the getWriter method has been called on this response .

    • This means that you can call either getWriter() or getOutputStream() methods.

    • Now in JSP (and eventually in compiled servlet), there is an implicit variable defined called out . This is nothing but an instance of PrintWriter class. This means that on the response object, getWriter() is already called and hence on calling getOutputStream() you get IllegalStateException

    • Now as solution for this problem, as some people have pointed out, move this code into a servlet where you have full control and use the outputstream the way you want.

    0 讨论(0)
提交回复
热议问题