Servlet sendRedirect

sendRedirect() is the method of the HttpServletResponse interface which is used to redirect response to another resource.

Syntax:

response.sendRedirect(relative url);

Difference between sendRedirect and RequestDispatcher.

             sendRedirect           RequestDispatcher
  1. Creates a new request from the client browser for the resource.
  2. Accept relative URLs so control can go inside or outside the server.
  3. The new URL can be seen in the browser.
  4. Work on the response object.
  1. No new request is created.
  2. Not accept relative URLs so can go only inside the server.
  3. The new URL can’t be seen in the browser.
  4. Work on the request object.

 

Example:

LoginServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
       
    //no-argument constructor
    public LoginServlet() {
        
    }
    
    protected void doPost(HttpServletRequest request, 
    	HttpServletResponse response) 
              throws ServletException, IOException {
    	
    	response.setContentType("text/html"); 
    	PrintWriter out = response.getWriter();
    	
    	//get parameters from request object.
    	String userName = 
    		request.getParameter("userName").trim();
    	String password = 
    		request.getParameter("password").trim();
    	
    	//check for null and empty values.
    	if(userName == null || userName.equals("") 
    			|| password == null || password.equals("")){
    		out.print("Please enter both username " +
    				"and password. ");
    		RequestDispatcher requestDispatcher = 
    			request.getRequestDispatcher("/login.html");
    		requestDispatcher.include(request, response);
    	}//Check for valid username and password.
    	else if(userName.equals("jai") && password.equals("1234")){
    		response.sendRedirect("WelcomeServlet");
    	}else{
    		out.print("Wrong username or password. ");
    		RequestDispatcher requestDispatcher = 
    			request.getRequestDispatcher("/login.html");
    		requestDispatcher.include(request, response);
    	}
	}
}

WelcomeServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
        //no-argument constructor
        public WelcomeServlet() {
       
        }

	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response)
	           throws ServletException, IOException {
	    	
	    	response.setContentType("text/html"); 
	    	PrintWriter out = response.getWriter();
	    	
	    	out.println("");
	    	out.println("You are logged in successfully.");

out.println(“”);

	}
}

login.html

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>


    <form action="LoginServlet" method="post">
        Username:<input type="text" name="userName">
        <br><br>
        Password:<input type="password" name="password">
        <br><br> 
        <input type="submit" value="login"> 
    </form>

 

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_2_5.xsd" id="WebApp_ID" version="2.5">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>
        com.w3schools.business.LoginServlet
    </servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>
        com.w3schools.business.WelcomeServlet
    </servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/WelcomeServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  
</web-app>