• 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

IllegalStateException Response has already been committed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using HttpServletResponse and ServletOutputStream. The code is as follows:

Actually when I click "Run Report" button in a html form, this method will be invoked. The actual requirement is after I click the "Run Report", .pdf should get downloaded and goto a different page. To achieve this I used response.sendRedirect() method. I got the following error:
java.lang.IllegalStateException: Response has already been committed, be sure not to write to the OutputStream or to trigger a commit due to any other action before calling this method.
Is there a way to redirect from a page to another when I used response.getOutputStream().
Also, if any errors occured, how do I show it. Actually in my code I am trying to get Writer object to show error messages. This will not work. Any ideas how to show error messages when response.getOutputStream() is used.
regds
-raju
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


java.lang.IllegalStateException: Response has already been committed, be sure not to write to the OutputStream or to trigger a commit due to any other action before calling this method.


The error message says it all...


Is there a way to redirect from a page to another when I used response.getOutputStream().


No. You've started writing a response; telling a browser to go somewhere else in the middle of it is a completely different response.
The answer to your problem is either to redirect the user to the file or to write the entire file back to the output stream yourself.


Also, if any errors occured, how do I show it. Actually in my code I am trying to get Writer object to show error messages. This will not work. Any ideas how to show error messages when response.getOutputStream() is used.


It's much more elegant from both the developers' and users' viewpoints to redirect to an error page and write the errors to a log. You can either forward or redirect the request/response using the RequestDispatcher.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
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