sendRedirect in Servlet

In this article we shall see how to redirect your current page to some other page. The easiest way to achieve this is by using the sendRedirect() method of class javax.servlet.http.HttpServletResponse.

sendRedirect() method redirects your current page to another servlet or jsp page. This method is used to redirect response to another resource. This method accepts relative as well as absolute URL.
Using sendRedirect() method whenever the client makes any request it goes to the container, then the container decides whether servlet can handle request or not. If it is not handled then that request cab be handled by other servlets.

Some important points on sendRedirect() method:

  • In the sendRedirect method existing request and response objects are lost.
  • sendRedirect methods happens on the client side.
  • In this method request gets transferred to resource outside the application.
  • sendRedirect method works on response object.
  • sendRedirect method is transfer to another resource to different domain.
  • In the sendRedirect method it call old request and response object is list because it is treated as new request.
  • sendRedirect method is slower because extra round trip is required because completely new request is created and old object request is lost.

sendRedirect Example

Now let us try an example as below. In this exmaple the sendRedirect() method would take you to the URL mentioned http://www.javabeat.net

Listing 1: SendRedirectExample

package javabeat.net.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendRedirectExample extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

				response.sendRedirect("http://www.javabeat.net");
	}
}

Listing 2:web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>SendRedirectExample</servlet-name>
		<servlet-class>javabeat.net.servlets.SendRedirectExample</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>SendRedirectExample</servlet-name>
		<url-pattern>/SendRedirectExample</url-pattern>
	</servlet-mapping>

</web-app>

Previous Tutorial : Request Dispatcher in Servlet  || Next Tutorial : ServletConfig in Servlet

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*