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:

 

login.html

 

uhome.html

 

Authenticate.java

 

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

 

Video: Redirection in Servlets

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me