sendRedirect in Servlet

In the previous article, we have discussed RequestDispatcher which dispatches the request from one page to another page or servlet. But if we want to take a response from one page to another what we will use. So in this case we have the sendRedirect method in servlet. In this article, we will discuss the sendRedirect() method.

What is sendRedirect?

The sendRedirect() method is of  HttpServletResponse Interface.This method is used to take responses from one page to another.sendRedirect() method can go for resources inside or outside the resources.

Let’s see the Example for sendRedirect() method in Servlet

In this example client request the server a query and it redirects it to https://codedec.com/ page.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="abc" method="get">
		Type here <input type="text" name="there"> <input
			type="submit" value="Click Here" id="button-1" />
	</form>
</body>
</html>

SendRedirectExample.java

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class SendRedirectExample
 */
@WebServlet("/SendRedirectExample")
public class SendRedirectExample extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public SendRedirectExample() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter printWriter = response.getWriter();
    String there = request.getParameter("there");
    response.sendRedirect("https://codedec.com/");

  }
}

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"
  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">
  <display-name>sendRedirectDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>SendRedirectExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/abc</url-pattern>
  </servlet-mapping>
</web-app>

Output

Difference between forward() method and sendRedirect() method

forward()

sendRedirect()

It works on the server-side. It works on both inside or outside servers.
It sends the same request and response It always sends new requests.
It can be used if you want to transfer control to the same domain It can be used if you want to transfer control to another domain.
request.getRequestDispatcher(“index.html”).

forward(request,response);

response.sendRedirect(“index.html”);