This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

sendRedirect method  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The foll: is my servlet:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class hello extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body> Hello World </body></html>");
pw.flush();
res.sendRedirect("http://www.cnn.com");

}
}

When I compile and run this class 'sendRedirect' method doesn't throw an "IllegalStateException". Sun spec. defines the foll:

"If the response has already been committed, this method throws an IllegalStateException."

Can somebody explain why it is not throwing an exception? Am I doing something wrong or does it really not throw an exception as it is supposed to?

Any reply would be really appreciated.
Thanks,
Jayanthi.
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were you redirected, or did you see "Hello World"?

When I do it, I get this (in the console):

And I see "Hello World" on my browser (no redirect).

Change yours to say "Hello World 2" and make sure you're running the right version, then check the console.

--Dale--
 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the effort of actually doing the program and suggesting a solution. I still do not get an 'IllegalStateException'. I do get 'Hello World'(or 'Hello World 2) on the browser and my console is absolutely neat and clean with no exceptions.
I normally get an 'IllegalStateException' on the browser(for eg. the session management egs. on pg.247 of HFS book) and here I get none.
But it is nice to know the program actually works the way it is supposed to!
Thanks again,
Jayanthi.
 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayanthi,
Which web server are u using, also what is the version of Servlet API you are using. Under normal circumstances you should get an Error.
Also make sure that the .class file of your servlet contains
res.sendRedirect("http://www.cnn.com");
may be you added this line later, and server still contains the older version.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!