File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes HttpServletResponse Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletResponse Error" Watch "HttpServletResponse Error" New topic
Author

HttpServletResponse Error

Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
In my servlet when I desire to send a user to a new page I use the following commands

res.setContentType("text/html");
res.sendRedirect("newpage.html");
res.flushBuffer();

Unfortunately some of the browsers do not respond well to these sequence of commands and are not redirected. Is there anyone here that knows why and has a work around
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, you're going to cause an IllegalStateException on the server.



You can't both write to the out stream and send a "sendRedirect".

In other words, if you're going to use sendRedirect, don't write anything to the page first (including a content-type header).
Also, as a rule of thumb, you should have a return statement just after the sendRedirect.

Example:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
I tried the following code but it did not work using Juno's browser.

// commented out res.setContentType("text/html");
// commented out PrintWriter out = res.getWriter();
res.sendRedirect("/index.html");
res.flushBuffer();
return;

Only the following code seems to work.

res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"></HTML>");
out.flush();
out.close();

Do you have any idea what the problem is?

[ April 03, 2005: Message edited by: Roland gray ]
[ April 03, 2005: Message edited by: Roland gray ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why are you calling flushBuffer after sendRedirect?
Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
Because when I did not used it the page did not get redirected as a result I thought that perhaps there was some sort of buffer issue. unfortunately it still does not work.
Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
The question on everyones mind should be the following;

Given that all other things are equal, and If you are using the HttpServletResponse object to redirect users, are your applications also failing for clients who uses Juno browsers to access your applications?
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
HI Ben,

What if I use forward instead of sendRedirect. Will I still get the same exception (IllegalStateException).

What is the difference between between the two, forward and sendRedirect.

Can I send parameters with sendRedirect.
With forward I can forward the request . CAn we also forward request with sendRedirect.

Due Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpServletResponse Error