Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

02/10/2016 Categories: Servlets. No Comments on Redirection in Servlets

In a web application it is common to redirect the user to a web page. For example on successful authentication of a user, he/she is redirected to user home page. Otherwise, redirected to login page again. We have to ways to redirect a user: one is using the sendRedirect() method available on response object and the second way is to use the forward() method available in RequestDispatcher interface.


sendRedirect() is executed on client side, whereas forward() is executed on server side. sendRedirect() works only with HTTP, whereas forward() works with any protocol. sendRedirect() takes two request and one response to complete, where as only one call is consumed in the case of the forward() method. sendRedirect() can be used to redirect a user to a resource on any server on the web. Whereas forward() can be used to redirect a user to a resource on the same server where the application or the page resides.


Following example demonstrates redirecting a user using sendRedirect() and forward() methods:






In the above example, if the user is authenticated, he/she is redirected to uhome.html page.


Video: Redirection in Servlets

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up