0% found this document useful (0 votes)
56 views

RequestDispatcher in Servlet - Javatpoint

The RequestDispatcher interface in Servlets provides the ability to dispatch requests to other resources like HTML, Servlets or JSPs. It has two methods - forward() to forward a request and include() to include the response of another resource. The getRequestDispatcher() method of the ServletRequest interface returns a RequestDispatcher object that can then use the forward() or include() methods to dispatch the request. An example is provided that validates a user's login by forwarding to a welcome servlet if correct, or displaying an error if incorrect.

Uploaded by

Satyam Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

RequestDispatcher in Servlet - Javatpoint

The RequestDispatcher interface in Servlets provides the ability to dispatch requests to other resources like HTML, Servlets or JSPs. It has two methods - forward() to forward a request and include() to include the response of another resource. The getRequestDispatcher() method of the ServletRequest interface returns a RequestDispatcher object that can then use the forward() or include() methods to dispatch the request. An example is provided that validates a user's login by forwarding to a welcome servlet if correct, or displaying an error if incorrect.

Uploaded by

Satyam Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

RequestDispatcher in Servlet
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,java.io.IOException:Forwards 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,java.io.IOException:Includes the content of a resource (servlet, JSP
page, or HTML file) in the response.

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.

https://www.javatpoint.com/requestdispatcher-in-servlet 2/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

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 the object of RequestDispatcher

The getRequestDispatcher() method of ServletRequest interface returns the object of


RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method

public RequestDispatcher getRequestDispatcher(String resource);  

Example of using getRequestDispatcher method

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

Example of RequestDispatcher interface

https://www.javatpoint.com/requestdispatcher-in-servlet 3/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

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.


Login.java file: a servlet class for processing the response. If password is servet, it will
forward the request to the welcome servlet.
WelcomeServlet.java file: a servlet class for displaying the welcome message.
web.xml file: a deployment descriptor file that contains the information about the
servlet.

index.html

<form action="servlet1" 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.*;  

https://www.javatpoint.com/requestdispatcher-in-servlet 4/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

import javax.servlet.http.*;  
  
  
public class Login 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");  
    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);  
                      
        }  
    }  
  
}  

WelcomeServlet.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class WelcomeServlet extends HttpServlet {  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
https://www.javatpoint.com/requestdispatcher-in-servlet 5/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

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

web.xml

<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>/servlet1</url-pattern>  
  </servlet-mapping>  
  <servlet-mapping>  
    <servlet-name>WelcomeServlet</servlet-name>  
    <url-pattern>/servlet2</url-pattern>  
  </servlet-mapping>  
  
  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
  </welcome-file-list>  
</web-app>  
https://www.javatpoint.com/requestdispatcher-in-servlet 6/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

download this example

download this example (developed in Myeclipse IDE)

download this example (developed in eclipse IDE)

download this example (developed in netbeans IDE)

https://www.javatpoint.com/requestdispatcher-in-servlet 7/11
11/21/22, 11:50 PM RequestDispatcher in Servlet - javatpoint

← Prev Next →


For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials

Splunk SPSS Swagger Transact-SQL

Tumblr ReactJS Regex Reinforcement


Learning

https://www.javatpoint.com/requestdispatcher-in-servlet 8/11

You might also like