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:
Really Great website for leaning java. thanks for this site.
Visit My site-
http://theonlinegk.wordpress.com/
Post a Comment