jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes about sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "about sendRedirect" Watch "about sendRedirect" New topic
Author

about sendRedirect

mohamed ewis
Greenhorn

Joined: Feb 05, 2006
Posts: 18
sendredirect : i know taht We cannot call this method if the response is committed�that is,if the response header has already been sent to the browser
but when i write it after commiting response no exception thrown ,why

PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>NewServlet at "
+ request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
response.sendRedirect("http://www.cnn.com");
Abi Ted
Greenhorn

Joined: May 24, 2008
Posts: 13
the response buffer is just filled.... it is not committed....

or out.flush() not called yet...
mohamed ewis
Greenhorn

Joined: Feb 05, 2006
Posts: 18
thanks Abinesh for your reply
manuly flush response and still no exception

PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>NewServlet at "
+ request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
out.flush(); // flush response
response.sendRedirect("http://www.cnn.com");
Abi Ted
Greenhorn

Joined: May 24, 2008
Posts: 13
i see a try{ in your program... what exception are you handling??

because some programmers handle it this way...
try{
}catch(Exception e){}

This is wrong...
Please post the catch also...
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
I tried this :


and it throws exception on console.

Thanks,
Amol


"There are no mistakes, only lessons"
mohamed ewis
Greenhorn

Joined: Feb 05, 2006
Posts: 18
hi amol ,thanks for reply
i tried the code as you wrote and no exceptions thrown ,
it out NewServlet at /JSPStudy in browser window
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: about sendRedirect