Forums Register Login

HttpServletResponse Error

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Why are you calling flushBuffer after sendRedirect?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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,
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1507 times.
Similar Threads
Is Mac part of the nix family?
Difference Between Grep,Fgrep & Egrep
Exclude sources in javac task
checksum
execute query
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:52:04.