티스토리 뷰

출처 : http://blog.naver.com/PostView.nhn?blogId=napsis&logNo=140156257733&parentCategoryNo=8&viewDate=&currentPage=1&listtype=0&from=postList


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();

'Java' 카테고리의 다른 글

파일업로드  (0) 2014.12.23
java Socket 관련 오류  (0) 2014.12.22
문자열 변환 차이  (0) 2014.11.08
request response 가져오기  (0) 2014.11.04
프레임워크없이 ajax 사용하기  (2) 2014.10.08
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함