• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java.lang.IllegalStateException:- when generating excel report

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am generating excel reports using jasper. I am able to generate excel report and providing save options user, it is workign fine but in logs I'm getting error.
error trace


and here is my code


Thanks.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't use JSP files for binary data. The problem is that any line break outside tags will be included in the response. This includes line breaks between import tags, etc. When such a line break is included in the response, this is written to the writer (as returned by getWriter()). As a result you can't call getOutputStream() anymore.

There are three solutions:
1) Simply don't include any line breaks. Let each tag follow the next tag. For instance, not but instead Each and any line break should be removed.

2) Not sure about this one, but perhaps you can turn on output buffering and clear the buffer before you start writing the Excel report.

3) Definitely the preferred way - use a Servlet instead of a JSP file. With servlets you have full control over what's printed out. If you don't call getWriter() explicitly then it's not called at all.
 
vardhan reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Thanks for your reply,
i have only one button in my jsp and my logic is in controller class only.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying you want both HTML (the button) and the Excel data in one single JSP file? Because that's definitely not going to work.
 
vardhan reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Rob,
i have button info in jsp and control login in java class.
I have checked my controller logic, but still i'm getting same error
and my jsp code is:
 
Ranch Hand
Posts: 129
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as I remember, you can't setHeaders after a call to getOutputStream().

 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin is right, after the response has been committed (actually it's being started to send the response to browser/some other resource) you can't add or change anything in response object.
 
vardhan reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin/Subramani,

I have updated setHeader before calling getOutputStream and still I am getting same error.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still say you should use a servlet instead of a JSP for generating the Excel file.
 
a wee bit from the empire
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic