Monday, March 16, 2020

RequestDispatcher in Servlet


RequestDispatcher in Java Servlets


RequestDispatcher Interface

The RequestDispatcher interface provides the capability of dispatching the request to another resource (such as a servlet, HTML file, or JSP file) on the server. RequestDispatcher interface wraps servlets, however, a servlet container can create RequestDispatcher to wrap any type of resource. The Servlet Container creates the object of RequestDispatcher to wrap server resources placed at a particular path or identified by any particular name.

The RequestDispatcher interface defines two important methods forward(ServletRequest request, ServletResponse response) and include(ServletRequest request, ServletResponse response).

public void forward(ServletRequest request, ServletResponse response)

This method forwards a request from one servlet to another resource like a servlet, a JSP file, or an HTML file on the server. This method throws ServletException,java.io.IOException.

forward method in RequestDispatcher
The forward Method


public void include(ServletRequest request, ServletResponse response)

This method includes the entire resource like a servlet, a JSP page, or an HTML file in the response. This method throws ServletException,java.io.IOException.

include method in RequestDispatcher
The include method

RequestDispatcher Example

In this example, we have four files.
index.html
index.html has a form containing a form with Select input and Submit button. The user can choose "RED", "BLUE" or "YELLOW" from the select option. If the user chooses RED, Servlet1 will forward the request to Servlet2, if the user chooses BLUE Servlet1 will forward the request to Servlet3.
Servlet1is YELLOW Servlet and handles the request from index.html.
Servlet2 is the RED Servlet.
Servlet3 is the BLUE Servlet.

RequestDispatcher

index.html


Welcome page with Color options to choose from a dropdown.

<html>
<head>
<title>welcome</title>
</head>
<body>
<form action="Servlet1" method="get">
<select name="color">
<option value='RED'>RED</option>
<option value='BLUE'>BLUE</option>
<option value='YELLOW'>YELLOW</option>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>

Servlet1.java

This servlet will handle the user request. The Servlet will forward to Red Servlet if option "RED" is chosen by the user. It will also display Blue Servlet included in itself if option "BLUE" is chosen, otherwise, it will show itself.

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

/**
 * Servlet implementation class Servlet1
 */
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
     private static final long serialVersionUID = 1L;
 
     public Servlet1() {
        super();
    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           PrintWriter pout=response.getWriter();
           response.setContentType("text/html");
           String color=request.getParameter("color");
          
          
           if(color.equalsIgnoreCase("RED"))
           {
                RequestDispatcher rd=request.getRequestDispatcher("Servlet2");
                rd.forward(request, response);
           }
           if(color.equalsIgnoreCase("BLUE"))
           {
                RequestDispatcher rd=request.getRequestDispatcher("Servlet3");
                rd.include(request, response);
           }
          
           pout.append("<!DOCTYPE html>\r\n" +
                     "<html>\r\n" +
                     "<head>\r\n" +
                     "<title>Servlet 1</title>\r\n" +
                     "</head>\r\n" +
                     "<body><h1 style='background-color:Yellow;'>YELLOW Servlet</h1>" +
                     "</body>\r\n" +
                     "</html>");
     }

}




Servlet2.java

This is Red Servlet.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
     private static final long serialVersionUID = 1L;

     public Servlet2() {
        super();
   
    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/html");
     response.getWriter().append("<html><body><h1 style='background-color:red;'>RED Servlet</h1></body></html>");
     }

}


Servlet3.java

This is Blue Servlet.


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

/**
 * Servlet implementation class Servlet3
 */
@WebServlet("/Servlet3")
public class Servlet3 extends HttpServlet {
     private static final long serialVersionUID = 1L;

     public Servlet3() {
        super();

    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           response.setContentType("text/html");
           response.getWriter().append("<html><body><h1 style='background-color:BLUE;'>BLUE Servlet</h1></body></html>");
     }

}





Result

index.html
index.html

Servlet1.java

RequestDispatcher Example
Select YELLOW
On selecting "RED" forward to Servlet2.java

RequestDispatcher Example
Select RED

On selecting "BLUE" include Servlet3.java in Servlet1.java(BLUE Servlet is included in YELLOW Servlet).

RequestDispatcher Example
Select BLUE