Requestdispacher Interface

Posted By on April 30, 2016

Download PDF
ServletConfig Interface
Servlet's Filter API

The RequestDispatcher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interface can also be used to include the content of another resource also. 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:

  1. public void forward(ServletRequest request,ServletResponse response)throws ServletException, a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
  2. public void include(ServletRequest request,ServletResponse response)throws ServletException, the content of a resource (servlet, JSP page, or HTML file) in the response.


How to get the object of RequestDispatcher

The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method

  1. public RequestDispatcher getRequestDispatcher(String resource);


Example of using getRequestDispatcher method

  1. RequestDispatcher rd=request.getRequestDispatcher(“servlet2”);
  2. //servlet2 is the url-pattern of the second servlet
  3. rd.forward(request, response);//method may be include or forward


Example of RequestDispatcher interface

In this example, we are validating the password entered by the user. If password is servlet, it will forward the request to the WelcomeServlet, otherwise will show an error message: sorry username or password error!. In this program, we are cheking for hardcoded information. But you can check it to the database also that we will see in the development chapter. In this example, we have created following files:

  • index.html file: for getting input from the user.
  • file: a servlet class for processing the response. If password is servet, it will forward the request to the welcome servlet.
  • file: a servlet class for displaying the welcome message.
  • web.xml file: a deployment descriptor file that contains the information about the servlet.

RequestDispatcher interface

ServletConfig Interface
Servlet's Filter API

Download PDF