이 응답에 대해 getOutputStream ()이 이미 호출되었습니다.
나는 오류 메시지를 구글 getOutputStream() has already been called for this response
많은 사람들이이 때문에 후 공백이나 줄 바꿈이다 없다고 말했다 <%
하거나 %>
,하지만 내 코드에서, 어떠한 공백이나 줄 바꿈이 있습니다. Linux에서 tomcat6을 사용하고 있습니다.
<%@
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)
근본 원인
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)
좋습니다 . JSP가 아닌 서블릿 을 사용해야합니다. 하지만 정말로 필요한 경우 페이지 상단에 다음 지시문을 추가하십시오.
<%@ page trimDirectiveWhitespaces="true" %>
또는 jsp-config 섹션에서 web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
또한 flush
/ 및 반환 할 때.close
OutputStream
dataOutput.flush();
dataOutput.close();
return;
여기서 문제는 JSP가 응답과 직접 대화하고 있다는 것 OutputStream
입니다. 이것은 기술적으로 금지되어 있지는 않지만 좋은 생각은 아닙니다.
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.
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.
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.
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.
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.
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.
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))
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()
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>
Use Glassfish 4.0 instead. This turns out to be a problem only in Glassfish 4.1.1 release.
PT-BR: Use o Glasfish 4.0. Este parece ser um problema apenas no Glassfish 4.1.1.
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.
ReferenceURL : https://stackoverflow.com/questions/1776142/getoutputstream-has-already-been-called-for-this-response
'programing tip' 카테고리의 다른 글
2 개의 열을 기반으로 SQL 고유 제약 조건을 생성하려면 어떻게해야합니까? (0) | 2020.12.30 |
---|---|
Threaded Django 작업이 트랜잭션 또는 db 연결을 자동으로 처리하지 않습니까? (0) | 2020.12.30 |
문자열에 점이있을 때 "is"키워드가 다른 동작을하는 이유는 무엇입니까? (0) | 2020.12.30 |
파이썬에서 오류가 없을 때까지 시도하십시오 (0) | 2020.12.30 |
C ++의 맵에서 첫 번째 값 가져 오기 (0) | 2020.12.30 |