Servlet - sendRedirect() Method with Example

Last Updated : 14 Jan, 2026

sendRedirect() is a method used to redirect the client from one resource to another by instructing the browser to make a new request to the specified URL.

  • It performs client-side redirection
  • Browser sends a new HTTP request
  • URL changes in the browser
  • Uses HTTP status code 302 (Found)

Method Signature

public void sendRedirect(String location) throws IOException

How sendRedirect() Works

  • Client sends a request to a servlet
  • Servlet processes the request
  • Servlet sends a redirect response (302) to the browser
  • Browser makes a new request to the redirected URL
  • Target resource sends the final response

Example: Using sendRedirect() in Java Servlet

In this example, we will create a simple Servlet project that uses the sendRedirect() method to redirect the client request to another website.

Step 1: Create a Dynamic Web Project

  • Open Eclipse IDE
  • Go to File -> New -> Dynamic Web Project
  • Enter the project name: SendRedirectExample
  • Select:
  • Target Runtime: Apache Tomcat
  • Dynamic Web Module version: 4.0
  • Click Finish

Step 2: Create index.html

  • Right-click WebContent
  • Select New -> HTML File
  • File name: index.html

index.html

Java
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>

    <form action="redirect" method="get">
        <h2>sendRedirect() method in Java Servlets</h2>

        <p>
            sendRedirect() method redirects the client request from one
            servlet to another resource. <br>
            It creates a new request from the client browser.
        </p>

        For More Information:
        <input type="submit" value="Click Here">
    </form>

</body>
</html>

Explanation

  • The form action is redirect
  • The request method is GET
  • This URL maps to the servlet with /redirect mapping
  • When the button is clicked, the servlet’s doGet() method executes

Step 3: Create Servlet Class

  1. Right-click src
  2. Select New -> Servlet
  3. Package name-> com.example.redirect
  4. Servlet name-> ServletRedirect
  5. URL Mapping-> /redirect
  6. Click Finish

Step 4: Servlet Code (ServletRedirect.java)

Java
package com.example.redirect;

import java.io.IOException;

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

@WebServlet("/redirect")
public class ServletRedirect extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.sendRedirect(
            "https://www.geeksforgeeks.org/java/url-rewriting-using-java-servlet/"
        );
    }
}

Explanation

  • @WebServlet("/redirect") maps the servlet URL
  • No need for web.xml configuration
  • sendRedirect() sends a 302 response to the browser
  • Browser makes a new request to the given external URL

Step 7: Run the Project

  • Right-click the project
  • Select Run As -> Run on Server
  • Choose Apache Tomcat
  • Click Finish

Step 8: Output

When the project runs, the browser displays.

http://localhost:8080/SendRedirectExample/index.html

ser1

When clicking for more information, we specified redirecting the response to that particular URL.

ser2

This way, we can use the sendRedirect() method in Servlets.


Comment