Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

August 2, 2010

Response : sendRedirect()

sendRedirect() method takes String as an argument. This string can start with a forward slash.

E.g.

Lets say the current URL we are accessing is : http://localhost:8080/MyApp/MyServlet/welcome.do

Now in our Servlet, we can do either :
response.sendRedirect("thanks.jsp");
OR
response.sendRedirect("/thanks.jsp");

Both of these would hit differnet URLs

In first case, i.e. response.sendRedirect("thanks.jsp");
The server will hit URL that is relative to the original URL, i.e http://localhost:8080/MyApp/MyServlet/thanks.jsp

and in second case,
the server builds the complete URL, relative to the web application itself.(instead of relative to original URL)
i.e.  http://localhost:8080/MyApp/thanks.jsp

One more thing to note here is that the argument to sendRedirect() is a String only and not an object and the URL should be something that the server can generate a complete URL from the relative. i.e. it should be relative to something (be it relative to current URL or to the web-app).
So, any argument like "www.pragyarawal.tk/welcome.jsp" would not work as the server would try to create a complete URL from it. and in this case, the server would throw an "IllegalStateException".

1 comment:

Ashu said...

Really Great website for leaning java. thanks for this site.

Visit My site-
http://theonlinegk.wordpress.com/