• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletResponse

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

How can I know if the response.getWriter() or response.getOutputStream() has already been called?


Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
as per servlet response API specification, if we will try to call one of the method response.getWriter() or response.getOutputStream() while the other one is already invoked then it will throw an IllegalStateException so if we want to know that the other method is already invoked, or we want to be safe before invoking response.getWriter() or response.getOutputStream(), put the code in a try/catch block, and don't put any other code inside this block.

like:

PrintWriter out=null;

try {
out = response.getWriter();
}
catch(IllegalStateException e)
{
System.out.println("response.getOutputStream() is already called");
}
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ghufran Ul Haq:
Hi

How can I know if the response.getWriter() or response.getOutputStream() has already been called?


Thanks



You have to review the code of the servlet for this.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I know if the response.getWriter() or response.getOutputStream() has already been called?



I guess you are concerned that the action you do on your response might throw an IllegalStateException cause response.getWriter() or response.getOutputStream() was already called. If that is the case, you can use response.isCommitted(). It returns true if the response has been committed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic