Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServletResponse Error

 
Roland gray
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Roland gray
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling flushBuffer after sendRedirect?
 
Roland gray
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic