RequestDispatcher in Servlet

RequestDispatcher in Servlet

In this article, I am going to discuss RequestDispatcher in Java Servlet Application. Please read our previous article where we discussed ServletRequest Interface. At the end of this article, you will understand the following pointers.

  1. RequestDispatcher in Servlet
  2. Include Mechanism
  3. Forward Mechanism
  4. Methods of RequestDispatcher Interface
  5. How to get the object of RequestDispatcher
  6. Example of RequestDispatcher
RequestDispatcher in Servlet

It provides the power of dispatching the request to a different resource it’s going to be HTML, servlet, or JSP. In web applications, we are ready to achieve web-component communication within the following 2 ways:

  1. Include Mechanism
  2. Forward Mechanism

If we would like to perform the above mechanisms internally, we must use the RequestDispatcher object. So that both include and forward mechanisms are commonly called as Request Dispatching Mechanisms.

Methods of RequestDispatcher Interface

It provides two methods. They are:

Include:

Include Request Dispatching mechanism can be used to include the target resource response into the present resource response. In the case of the Include mechanism, when we send a request to the first resource then the container will prepare request and response objects, by executing some part in the first resource container may generate some response in the response object.

public void include(ServletRequest req, ServletResponse res) throws ServletException, IOException

Forward:

In web applications, the main purpose of the Forward mechanism is to forward requests from the present resource to the target resource. In the case of the Forward mechanism, when we send a request to the first resource then the container will create request and response objects, executing some part in the first resource container may generate some response in the response object.

public void forward(ServletRequest req, ServletResponse res) throws ServletException, IOException

How to get the object of RequestDispatcher

getRequestDispatcher() method returns the object of RequestDispatcher.

Syntax: public RequestDispatcher getRequestDispatcher(String resource);

Example of RequestDispatcher
index.html
<!DOCTYPE html>
<html>
<head>
 <meta charset="ISO-8859-1">
 <title>Insert title here</title>
</head>

<body>
 <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>
</body>
</html>
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");
        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) throws ServletException, IOException
    {
        response.setContentType ("text/html");
        PrintWriter out = response.getWriter ();
        String n = request.getParameter ("userName");
        out.print ("Welcome " + n);
    }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>

Output

RequestDispatcher in Java Servlet Application

RequestDispatcher in Servlet Application

In the next article, I am going to discuss Servlet chaining in Java Application. Here, in this article, I try to explain RequestDispatcher in Servlet. I hope you enjoy this RequestDispatcher in Servlet article.

Leave a Reply

Your email address will not be published.