getOutputStream() has already been called for this response

JavaJspTomcat

Java Problem Overview


I google the error message getOutputStream() has already been called for this response and many people said it is because of the space or newline after <% or %>, but in my code , there is no a space or a newline. I am using tomcat6 on 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)

root cause

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)

Java Solutions


Solution 1 - Java

Ok, you should be using a servlet not a JSP but if you really need to... add this directive at the top of your 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 the OutputStream and return when done.

dataOutput.flush();
dataOutput.close();
return;

Solution 2 - Java

The issue here is that your JSP is talking directly to the response OutputStream. This technically isn't forbidden, but it's very much not a good idea.

Specifically, you call response.getOutputStream() and write data to that. Later, when the JSP engine tries to flush the response, it fails because your code has already "claimed" the response. An application can either call getOutputStream or getWriter on any given response, it's not allowed to do both. JSP engines use getWriter, and so you cannot call getOutputStream.

You should be writing this code as a Servlet, not a JSP. JSPs are only really suitable for textual output as contained in the JSP. You can see that there's no actual text output in your JSP, it only contains java.

Solution 3 - Java

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

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

As has been noted, this isn't best practice, but it avoids the errors in your logs.

Solution 4 - Java

I had this problem only the second time I went to export. Once I added:

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

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

Solution 5 - Java

Here is what worked for me in similar case.

After you finish writing into the Servlet OutputStream just call response.sendRedirect("yourPage.jsp");. That would cause initiation of a new request from the browser, therefore avoid writing into the same output stream.

Solution 6 - Java

I just experienced this problem.

The problem was caused by my controller method attempting return type of String (view name) when it exits. When the method would exit, a second response stream would be initiated.

Changing the controller method return type to void resolved the problem.

I hope this helps if anyone else experiences this problem.

Solution 7 - Java

JSP is s presentation framework, and is generally not supposed to contain any program logic in it. As skaffman suggested, use pure servlets, or any MVC web framework in order to achieve what you want.

Solution 8 - Java

This error was occuring in my program because the resultset was calling more columns to be displayed in the PDF Document than the database contained. For example, the table contains 30 fields but the program was calling 35 (resultset.getString(35))

Solution 9 - Java

I got the same error by using response.getWriter() before a request.getRequestDispatcher(path).forward(request, response);. So start works fine when I replace it by response.getOutputStream()

Solution 10 - Java

I got the same problem, and I solved just adding "return;" at the end of the 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>

Solution 11 - Java

I didn't use JSP but I had similar error when I was setting response to return JSON object by calling PrintWriter's flush() method or return statement. Previous answer i.e wrapping return-statement into a try-block worked kind of: the error disappeared because return-statement makes method to ignore all code below try-catch, specifically in my case, line redirectStrategy.sendRedirect(request, response, destination_addr_string) which seem to modify the already committed response that causes the error. The simpler solution in my case was just to remove the line and let client app to take care of the redirection.

Solution 12 - Java

I faced the same issue, this issue occurs because Java gives an error if you use multiple writer/output stream using getWriter()/getOutputStream().

For me the issue was I was using @Logging and @RestLogging, avoid using extra annotation while calling the API.

Solution 13 - Java

In my case I am using the out object for printing the code It shows the error like getOutputStream() has already called for the response. For resolving this error, I am initializing another PrintWriter object out1 and use that for printing the code That's working perfectly. Eg. Before: out.println("Error"); After: PrintWriter out1 = response.getWriter(); out1.println("Success");

Solution 14 - Java

In some cases this case occurs when you declare

Writer out=response.getWriter   

after declaring or using RequestDispatcher.

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

Try defining Writer class object before RequestDispatcher class.

Solution 15 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSouthsouthView Question on Stackoverflow
Solution 1 - JavaRealHowToView Answer on Stackoverflow
Solution 2 - JavaskaffmanView Answer on Stackoverflow
Solution 3 - JavapnairnView Answer on Stackoverflow
Solution 4 - JavamichaelpView Answer on Stackoverflow
Solution 5 - JavaIgorView Answer on Stackoverflow
Solution 6 - JavaDan TorreyView Answer on Stackoverflow
Solution 7 - JavaBozhoView Answer on Stackoverflow
Solution 8 - JavaWandile NxumaloView Answer on Stackoverflow
Solution 9 - JavaDaniel De LeónView Answer on Stackoverflow
Solution 10 - Javadiego matos - kekeView Answer on Stackoverflow
Solution 11 - JavaVeli-Matti SorvalaView Answer on Stackoverflow
Solution 12 - JavaCodingBeeView Answer on Stackoverflow
Solution 13 - JavaSherinView Answer on Stackoverflow
Solution 14 - JavaAman KumarView Answer on Stackoverflow
Solution 15 - JavaMaxView Answer on Stackoverflow