This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Other Java Products and Servers and the fly likes java.lang.IllegalStateException:- when generating excel report Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "java.lang.IllegalStateException:- when generating excel report" Watch "java.lang.IllegalStateException:- when generating excel report" New topic
Author

java.lang.IllegalStateException:- when generating excel report

vardhan reddy
Ranch Hand

Joined: Oct 28, 2011
Posts: 42
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vardhan reddy
Ranch Hand

Joined: Oct 28, 2011
Posts: 42
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

Joined: Oct 27, 2005
Posts: 19538
    
  16

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

Joined: Oct 28, 2011
Posts: 42
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:
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

As long as I remember, you can't setHeaders after a call to getOutputStream().

Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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.


No pain, No gain.
OCJP 1.6
vardhan reddy
Ranch Hand

Joined: Oct 28, 2011
Posts: 42
Nitin/Subramani,

I have updated setHeader before calling getOutputStream and still I am getting same error.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

I still say you should use a servlet instead of a JSP for generating the Excel file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.IllegalStateException:- when generating excel report
 
Similar Threads
Generate excel reports using jasper report
Application is executing correctly but getting these exception in log
Spring ModelMap rendering problem
getting number format exception on jsp
JasperReports + JSP