• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletResponse  RSS feed

 
Ghufran Ul Haq
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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


Thanks
 
Ashish Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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");
}
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!