Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Response.sendRedirect()

 
Ajai
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am facing one problem regarding response.sendRedirect . I am committing the response by using out.flush() , and then redirecting to some jsp file . In my opinion , it should throw IllegalStateException , but it does not.
Please help.
code is as follows:-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
PrintWriter out = response.getWriter();
out.flush();
if(response.isCommitted() )
{
out.write("response is committed");//prints this line
}
response.sendRedirect("/JSP/time.jsp");
return ;
}
Thanks ....
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure out.flush() will really flush the content. Try response.flushBuffer() instead. Below is the method description.
public void flushBuffer() throws java.io.IOException
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.
[ August 18, 2002: Message edited by: ks wong ]
 
Anselme Bender
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API documentation :
public java.io.PrintWriter getWriter() throws java.io.IOException
Returns a PrintWriter object that can send character text to the client. The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.
If necessary, the MIME type of the response is modified to reflect the character encoding used.
Calling flush() on the PrintWriter commits the response.
[...continued...]
A. Bender
 
Ajai
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wong and Bender for ur reply,
I have modified my servlet as per ur suggestions :-
response.setContentType("text/html;charset=ISO-8859-4");
java.io.PrintWriter out = response.getWriter();

response.flushBuffer();
if(response.isCommitted() )
{
out.write("response is committed");
}
response.sendRedirect("/JSP/time.jsp");
But the result is still the same , it just prints "response is committed"
Thanks,
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buggi
According to the API A commited response has already had its status code and headers written.. So try to set the status code and then flush it.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buggi,
which version of tomcat are you using? Have you checked the tomcat log file catalina.log? My guess is you would see the exception thrown right there.
Check it, and let us know.
[ August 19, 2002: Message edited by: Roseanne Zhang ]
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buggi,
You said you got "response is committed". That means, flush/flushBuffer did change the state to "committed".
Since you didn't catch any exception, Web container will do it for you. Check your console and log file.
 
Ajai
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All ,
Yes IllegalStateException gets caught if I use a try block , and it prints it on the console also.
I was assuming that if exception is propagated to container it will automatically throw some exception message on browser which it did not , it just got stuck and showed a blank page . But it does show exception message on browser if I use sendError .
Thanks once again ...
Buggi.
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
If container handles this exception, what are all the other exceptions that container will handle ??.
Thanks & Regarda
Raj Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic