HttpServletResponse sendRedirect() method:-



sendRedirect() method is used to redirect client request to some other servlet, jsp or html file. It has the following syntax -
public void sendRedirect(String resourceURL) throws IOException    

Where 
    resourceURL - is the servlet, jsp or html file , where client request will be redirected.   

This method in addition to absolute url, can also accept relative url.

Relative URL -

Instead of specifying full url, we can also specify relative resourceURL in sendRedirect().
For example - suppose client make the following request - "http://www.gettechnotes.com/MyProject/servs/servlet1".
And servlet1 uses a sendRedirect() to forward client request to servlet2 contained in package "test". sendRedirect() can use the relative url to forward request to servlet2. And it can use it in one of the following two ways -
1- With /(forward slash) -
sendRedirect("/test/servlet2");
In this case server will use the complete url relative to your project web-app. So the url used by server will be - "http://www.gettechnotes.com/MyProject/test/servlet2".

2- Without /(forward slash) -
sendRedirect("test/servlet2");
In this case server will use the complete url relative to your current request url. So the url used by server will be - "http://www.gettechnotes.com/MyProject/servs/test/servlet2".

sendRedirect() works as shown below -


Fig: Working of sendRedirect() method

Example -

In this example, default page (index.html) will ask for username and password, if we enter correct username and password, it will redirect us to Home page, otherwise it will again show you index.html page with an error message.
1- index.html or index.jsp - This will be default page.
<html>
    <head>
        <title>Login Form</title>
    </head>
    <body>
     <form method="post" action="LoginCheck">
     <table border="1px">
      <tr>
        <td>User Name: </td>
        <td><input type="text" name="t1"></td>
      </tr>
      <tr>
        <td>Password </td>
        <td><input type="password" name="t1"></td>
      </tr>
      <tr>
        <td><input type="submit" value="Login"></td>
        <td><input type="reset" value="Reset"></td>
      </tr>
                
    </table>
    </form>
    </body>
</html>
  
This will create a login form with two fields Username and Password, When we click the Login button it will redirect us to LoginChek servlet.
2- LoginCheck servlet - Create a Package named "servs" and within "servs", create a Servlet named "LoginCheck".
LoginCheck servlet will get the username and password entered by user, and if user enters the valid username and password (username = admin and password = 123 in our example) then redirected to Home servlet, otherwise it include index.html in LoginCheck servlet.
package servs;

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


public class LoginCheck extends HttpServlet {


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try
        {
            PrintWriter out = response.getWriter();
            String nm = request.getParameter("t1");
            String ps = request.getParameter("t2");
            if(nm.equals("admin") && ps.equals("123"))
            {
                response.sendRedirect("Home");
            }       
            else
            {
                out.println("<p style='color:red'>Wrong username or password</p>");
                RequestDispatcher rdp = request.getRequestDispatcher("index.html");
                rdp.include(request, response);
            }
        }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
    }
}

3- Home servlet Within servs create servlet named "Home".
package servs;

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

public class Home extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        try
        {
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Home</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Weclome to Home</h1>");
            out.println("</body>");
            out.println("</html>");
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}    

4- web.xml - Add the following code to web.xml within web-app
<servlet>
  <servlet-name>LoginCheck</servlet-name>
  <servlet-class>servs.LoginCheck</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>LoginCheck</servlet-name>
  <url-pattern>/LoginCheck</url-pattern>
</servlet-mapping>
<servlet>
  <servlet-name>Home</servlet-name>
  <servlet-class>servs.Home</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Home</servlet-name>
  <url-pattern>/Home</url-pattern>
</servlet-mapping>    

5- Now run your project, and it will show you index.html page, enter username and password and click Login button.



When we enter correct user name and password, it will display following output -


Notice the URL in browser window, It is showing "Home".
When we click Login button, we send a request to LoginCheck servlet, but LoginCheck updated our browser by sending a response that request should go to Home servlet, Browser creates a new request with Home and finally we get response from Home servlet.
Whereas in case of forward() method of RequestDispatcher, Browser was showing the URL of LoginCheck servlet and output in browser was of Home servlet.

If we enter incorrect username or password it will display the following output -