GetOutputStream () has already been called for this answer

I google the error message getOutputStream() has already been called for this response and many said it was due to a space or a new line after <% or %> , but there is no space or new line in my code. I am using tomcat6 for linux.

 <%@ page import="java.servlet.*, javax.servlet.http.*, java.io.*, java.util.*, com.lowagie.text.pdf.*, com.lowagie.text.*" %><% response.setContentType("application/pdf"); Document document = new Document(); try{ ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PdfWriter.getInstance(document, buffer); document.open(); PdfPTable table = new PdfPTable(2); table.addCell("1"); table.addCell("2"); table.addCell("3"); table.addCell("4"); table.addCell("5"); table.addCell("6"); document.add(table); document.close(); DataOutput dataOutput = new DataOutputStream(response.getOutputStream()); byte[] bytes = buffer.toByteArray(); response.setContentLength(bytes.length); for(int i = 0; i < bytes.length; i++) { dataOutput.writeByte(bytes[i]); } }catch(DocumentException e){ e.printStackTrace(); } %> 

~

 org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

The main reason

 java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.catalina.connector.Response.getWriter(Response.java:610) org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) 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:188) org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118) org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77) org.apache.jsp.Account.Domain.testPDF_jsp._jspService(testPDF_jsp.java:94) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
+53
java jsp tomcat
Nov 21 '09 at 17:37
source share
12 answers

Well, you should use a non-JSP servlet , but if you really need to ... add this directive to the top of the page:

 <%@ page trimDirectiveWhitespaces="true" %> 

Or in the jsp-config section your web.xml

 <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <trim-directive-whitespaces>true</trim-directive-whitespaces> </jsp-property-group> </jsp-config> 

Also flush / close OutputStream and return when done.

 dataOutput.flush(); dataOutput.close(); return; 
+48
Nov 21 '09 at 18:16
source share

The problem here is that your JSP speaks directly with the OutputStream response. This is not technically prohibited, but it is a very not a good idea.

In particular, you call response.getOutputStream() and write the data. Later, when the JSP engine tries to discard the response, it fails because your code has already "requested" the answer. An application can either call getOutputStream or getWriter for any given response, it is not allowed to execute both. JSP engines use getWriter , so you cannot call getOutputStream .

You should write this code as a servlet, not a JSP. JSPs are really only suitable for the text output contained in the JSP. You can see that there is no actual text output in your JSP, it only contains java.

+35
Nov 21 '09 at 17:43
source share

Add the following to the end of try / catch to avoid the error that occurs when the JSP engine resets the response via getWriter ()

 out.clear(); // where out is a JspWriter out = pageContext.pushBody(); 

As already noted, this is not the best practice, but it avoids errors in your logs.

+9
Sep 07 '12 at 7:23
source share

I had this problem only the second time I went for export. As soon as I added:

 response.getOutputStream().flush(); response.getOutputStream().close(); 

after export was complete, my code started working all the time.

+4
Aug 27 '14 at 16:25
source share

Here's what worked for me in a similar case.

After you finish writing to the Servlet OutputStream just call response.sendRedirect("yourPage.jsp"); . This will trigger a new request in the browser, so do not write it to one output stream.

+3
Apr 04 2018-12-12T00:
source share

I just experienced this problem.

The problem is because my controller method is trying to return the String type (view name) when it exits. When the method completes, a second response thread will be initiated.

Changing the type of the returned controller method to void fixes the problem.

Hope this helps if anyone else comes across this issue.

+3
Jun 19 '14 at 16:33
source share

JSP is a presentation presentation environment and usually should not contain any programming logic. As the scaffman suggested, use clean servlets or any MVC web infrastructure to achieve what you want.

+2
Nov 21 '09 at 17:57
source share

This error occurred in my program because the result set caused more columns to be displayed in the PDF document than the database it contained. For example, a table contains 30 fields, but the program called 35 (resultset.getString (35))

0
Jul 09 '13 at 9:03 on
source share

I got the same error using response.getWriter() before request.getRequestDispatcher(path).forward(request, response); . So getting started is fine when I replace it with response.getOutputStream()

0
Jan 20 '17 at 0:31
source share

I had the same problem and decided to just add a β€œrefund”; at the end of FileInputStream.

Here is my jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.*"%> <%@ page trimDirectiveWhitespaces="true"%> <% try { FileInputStream ficheroInput = new FileInputStream("C:\\export_x_web.pdf"); int tamanoInput = ficheroInput.available(); byte[] datosPDF = new byte[tamanoInput]; ficheroInput.read(datosPDF, 0, tamanoInput); response.setHeader("Content-disposition", "inline; filename=export_sise_web.pdf"); response.setContentType("application/pdf"); response.setContentLength(tamanoInput); response.getOutputStream().write(datosPDF); response.getOutputStream().flush(); response.getOutputStream().close(); ficheroInput.close(); return; } catch (Exception e) { } %> </body> </html> 
0
Mar 04 '19 at 16:37
source share

Use Glassfish 4.0 instead. This turns out to be a problem only in the release of Glassfish 4.1.1.

PT-BR: Use o Glasfish 4.0. Este parece ser um problema apenas no Glassfish 4.1.1.

-one
May 30 '17 at 9:13 p.m.
source share

In some cases, this case occurs when you announce

 Writer out=response.getWriter 

after declaring or using RequestDispatcher .

I encountered a similar problem when creating a simple LoginServlet , where I defined Writer after declaring RequestDispatcher .

Try defining an object of class Writer to class RequestDispatcher .

-one
Dec 27 '17 at 12:47 on
source share



All Articles