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

Listing 1: SendRedirectExample



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 {


Listing 2:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="3.0">




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

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!