출처: http://november11tech.tistory.com/88 [Mr.november11] [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause :: 사월은 봄이다.

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

STATUS : Closed


Scenario :

jsp to generate a report and on click of a button download the report xls from the server. Code in the jsp is as follows.


response.setContentType("application/vnd.ms-excel");

response.setHeader("Content-Disposition",

"attachment; filename=Report.xls");


OutputStream out1 =response.getOutputStream();


WritableWorkbook workbook = Workbook.createWorkbook(out1);


Resolution :

Moved the code in the jsp to a servelet



Possible RootCause :

You cannot get a handle to the multiple output stream objects. When the jsp code gets compiled to plain java it adds code " out.write() " to write the jsp content to the outputstream. Since you already have got a handle to the ouputstream the out.write() system code throws the exception. Hence moving the code to a servelet solves the issue.

 

 

 

out.clear();


out=pageContext.pushBody(); 


 


 


 


 


요 두줄을 OutputStream하기 전에 넣어주면 되겠다.  out은 jsp 웹페이지 자신을 가리키는 것 ! ! !


jsp에서는 servlet으로 변환될때 내부적으로 out 객체가 자동으로 생성되기 때문에 따로 out 객체를 만들면 충돌이 일어나서 저런 메시지가 뜨는 것이라고 한다.


 


 


 out.clear(); 

  

 out=pageContext.pushBody();


 

 BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));   

 OutputStream outs = response.getOutputStream();  


 

'개발 > JSP_WEB' 카테고리의 다른 글

request.getSession()  (0) 2018.02.12
절대경로랑 상대경로  (0) 2018.02.12
request.getParameter()과 request.getAttribute()  (0) 2018.02.12
str.getBytes  (0) 2018.02.12
익스프레션 언어  (0) 2018.02.12

+ Recent posts