what is request redirection in servlet?

In this post, you will learn:

  • What is request redirection?
  • What is the difference between redirection and forwarding?
  • How is request redirection performed?

Request redirection vs Request Forwarding

Two understand the difference between request redirection and forwarding, lets take the following analogy. Lets assume that in Tech Mentro, Mr. X is the receptionist, Mr. Y is the manager and Mr. Z was the former manager who now works in some other company. A person calls to talk to the manager. Mr. X who receives the call, forwards it to Mr. Y. After some time another person calls to talk to Mr. Z. This time Mr. X informs the caller that Mr. Z is no longer here and provides his personal number to the caller. The caller dials this number and talks to Mr. Z. The first call is the example of forwarding and the second call is the example of redirection. In the second case, the caller need to make two calls to talk to Mr. Z because he was not available at the first place.

In case of forwarding, A client sends a request which is processed by one servlet and then forwarded to another servlet of the same or different application on the same server. The client receives the response from the forwarded servlet.

In case of redirection, Client sends a request in the response of which is a redirection url of different server is received. Using this url the client sends another request to receive the expected response.

Following diagram describes the request flow of redirection:

request redirection

Description of the diagram:

1.0 – Client sends a request for an application on server1.

1.1 – URL of another application is sent to the client as response by the server1 application.

1.2 – A new request is auto submitted by the client browser to the server2 using the received URL.

1.3 – Expected response is sent to the client by the server2 app.

Scenarios in which redirection is used:

1. When the requested resource is moved to another location e.g. If the domain of a site is changed then on the old domain a redirection page is created to redirect the clients who know the old domain to the new one.

2. When more than one sites participate in the processing of a request e.g. in online payments, a vendor site redirects the customers to a payment gateway site which redirects them back to the vendor’s site after the transaction is completed.

3. When third party authentication of users is performed. Login through facebook, gmail and twitter accounts etc are example of third party authentication. In all these cases, vendors site redirects the customer to facebook, gmail or twitter where authentication is performed and the user is redirected back along with some token to the vendor’s site.

How to redirect request from a servlet:

The HttpServletResponse interface provides following methods for request redirection:

public void sendRedirect(String redirectionUrl) throws IOException;

Practical example of request redirection:

First, the single line index.html :

Redirect me to Tech Mentro: learning Pad

Second, the RedirectingServlet :

package com.techmentro.learningpad;

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

public class RedirectingServlet extends HttpServlet
{
	public void doGet(
			HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		//Redirection URL which is provided as initialization parameter, is obtained from the ServletConfig 
		String url=getServletConfig().getInitParameter("redirectTo");
		//Sending the redirection URL as response
		response.sendRedirect(url);
		
	}

}

Third, the web.xml. Redirection URL is provided to the servlet from web.xml as initialization parameter:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.RedirectingServlet</servlet-class>
    <init-param>
    <param-name>redirectTo</param-name>
    <param-value>http://www.techmentro.com/learningpad</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>s1</servlet-name>
    <url-pattern>/redirectingServlet</url-pattern>
  </servlet-mapping>
</web-app>

When you will run this applicaiton, just notice the change of URL in the browser from http://localhost:portNo/appName/redirectingServlet to http://techmentro.com/learningpad. This change of URL represent that two requests are sent by the browser.


Its time to recap the main concepts:

  1. Forwarding is internal, redirection is external.
  2. Forwarding requires one request, where as redirection requires at least two requests.
  3. sendRedirect() method of HttpServletResponse interface is used for request redirection in servlet.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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