java.lang.IllegalStateException: Already using output stream [closed]

孤街浪徒 提交于 2019-11-28 09:31:09
Ramesh PVK

You cannot use both getServletOutputStream() and getWriter() in same response.

Coming to your problem. Avoid writing scriptlets in JSP. Whatever you are doing in JSP , implement it in Servlet.

You are calling response.getOutputStream(); in JSP which is illegal. You should use either ServletResponse.getOutputStream() or ServletResponse.getWriter(). Since JSP's use ServletResponse.getWriter() by default. You should write to ServletResponse.getWriter() instead ServletResponse.getOutputStream()

This is what Java Doc says :

getOutputStream...

ServletOutputStream getOutputStream() throws IOException

Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data.

Calling flush() on the ServletOutputStream commits the response. Either this method or getWriter() may be called to write the body, not both.

Returns: a ServletOutputStream for writing binary data Throws: IllegalStateException - if the getWriter method has been called on this response

Seems like the exception is thrown from this line.

out.println(e);

In case your code to send the PDF as Application fails during writing then it will thrown and exception and while trying to print the exception with the above line the outputputstream is already being used.

Do not intermix UI and Business Logic in JSP. Use a Servlet to do this job.

You have </body> after your scriptlet. It'll print something to the response outputstream, but you have already closed that one before.

What sense does it make to include <body> tags in your response when what you actually want to do is stream a PDF back to the client?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!