RequestDispatcher in Servlet

The RequestDispacher interface requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object. It is one of the way of servlet collaboration.

There are two methods defined in the RequestDispatcher interface.

Methods of RequestDispatcher interface

The RequestDispatcher interface provides two methods. They are:

Method Description
voidforward(ServletRequest request, ServletResponse response) forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server
voidinclude(ServletRequest request, ServletResponse response) includes the content of a resource (servlet, JSP page, HTML file) in the response

Life cycle of a servlet

As you see in the above figure, response of second servlet is sent to the client. Response of the first servlet is not displayed to the user.

requestdispatcher-in-servlet2

As you can see in the above figure, response of second servlet is included in the response of the first servlet that is being sent to the client.

How to get an Object of RequestDispatcher

getRequestDispatcher() method of ServletRequest returns the object of RequestDispatcher.

Syntax: RequestDispatcher rs = request.getRequestDispatcher("hello.html"); rs.forward(request,response); requestdispatcher-in-servlet

Use method of include

RequestDispatcher rs = request.getRequestDispatcher("hello.html"); rs.include(request,response); requestdispatcher-in-servlet

Example of RequestDispatcher interface

In this example, we will show you how RequestDispatcher is used to forward or include response of a resource in a Servlet. Here we are using index.html to get username and password from the user, ValidateServlet will validate the password entered by the user, if the user has entered "javatportal" as password, then he will be forwarded to WelcomeServlet else the user will stay on the index.html page and an error message will be displayed.

  • index.html file: for getting input from the user.
  • Login.java : a servlet class for processing the response. If password is servet, it will forward the request to the welcome servlet.
  • WelcomeServlet.java : a servlet class for displaying the welcome message.
  • web.xml file: a deployment descriptor file that contains the information about the servlet.
requestdispatcher-in-servlet index.html
	
<form action="servlet" method="post">  
Name:<input type="text" name="userName"/><br/>  
Password:<input type="password" name="userPass"/><br/>  
<input type="submit" value="login"/>  
</form> 
				
Login.java
import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  

public class Login extends HttpServlet {  
public void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
  response.setContentType("text/html");  
  rintWriter out = response.getWriter();  
  String n=request.getParameter("userName");  
  String p=request.getParameter("userPass");  
  if(p.equals("servlet"){  

        RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
       rd.forward(request, response);  
    }  
    else{  
        out.print("Sorry UserName or Password Error!");  
        RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
       rd.include(request, response);  
}
}
}
				
import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
public class WelcomeServlet extends HttpServlet {  

    public void doPost(HttpServletRequest request, HttpServletResponse response)  
       throws ServletException, IOException {  
  response.setContentType("text/html");  
  PrintWriter out = response.getWriter();  
 String n=request.getParameter("userName");  
  out.print("Welcome "+n);  
    }  

}	

	
	

<web-app>  
 <servlet>  
    <servlet-name>Login</servlet-name>  
   <servlet-class>Login</servlet-class>  
  </servlet>  
  <servlet>  
    <servlet-name>WelcomeServlet</servlet-name>  
    <servlet-class>WelcomeServlet</servlet-class>  
  </servlet>  

  <servlet-mapping>  
    <servlet-name>Login</servlet-name>  
    <url-pattern>/servlet</url-pattern>  
  </servlet-mapping>  
  <servlet-mapping>  
    <servlet-name>WelcomeServlet</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  

  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
  </welcome-file-list>  
</web-app>