File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Response.sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Response.sendRedirect()" Watch "Response.sendRedirect()" New topic
Author

Response.sendRedirect()

Ajai
Ranch Hand

Joined: May 29, 2002
Posts: 198
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

Joined: Jun 25, 2002
Posts: 104
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 ]

SCEA | SCBCD | SCWCD | SCJP - The kSpace
Anselme Bender
Greenhorn

Joined: May 22, 2002
Posts: 21
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


Anselme Bender<br />SCJP2
Ajai
Ranch Hand

Joined: May 29, 2002
Posts: 198
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

Joined: May 10, 2001
Posts: 3244
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.


Dave
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Jun 25, 2002
Posts: 104
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

Joined: May 29, 2002
Posts: 198
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

Joined: Jul 09, 2002
Posts: 77
Hi Everybody,
If container handles this exception, what are all the other exceptions that container will handle ??.
Thanks & Regarda
Raj Paul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Response.sendRedirect()