In this tutorial you will learn about the Servlet RequestDispatcher and its application with practical example.
Servlet RequestDispatcher Interface
This interface defines an object that receives request from the client and sends them to any resource which can be Servlet,HTML or JSP. This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resources.
RequestDispatcher Interface Methods
There are two methods which are provided by the RequestDispatcher interface.
public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException :- This method is used to forwards a request from a servlet to another resource such as servlet, JSP, or HTML file.
public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:- This method is used to includes the content of a resources (like servlet, JSP or HTML file) in the response.
RequestDispatcher Method
Syntax:-
The getRequestDisptacher() method is used to returns the object of RequestDisptacher and the syntax is shown below.
1 |
RequestDispatcher getRequestDispatcher(String resource); |
Example:-
Let’s understand this interface by an example. The files which are needed for this example are :
index.html:- From this page we are going to send request to servlet.
Login.java (Servlet) :- This is servlet class , which will process the response. If username And Password are matched then it will forward the request to Home.java servlet.
Home.java(Servlet):- This servlet will display the Welcome message to user.
web.xml file:- This is a deployment descriptor file which is used to contain information about the servlet, welcome file etc.
Check below code for creating this file.
index.html :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>W3Adda.com</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="Login" method="post"> <p>Name:<input type="text" name="uName"></p> <p>Password:<input type="password" name=uPass></p> <p><input type="submit" value="Submit"></p> </form> </body> </html> |
login.java :-
You can check that we have send request using post method from index.hmtl file. SO here it is necessary to get data in doPost() method of servlet. We will make condition for checking user name and password here, if it is correct so using getRequestDispatcher() method we will send request to Next Servlet which is Home in our case.
Syntax:-
1 2 3 |
//Syntax which we have used for that is: RequestDispatcher rd=request.getRequestDispatcher("/index.html"); rd.include(request, response); |
You can also check it in below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Login extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uName=request.getParameter("uName"); String uPass=request.getParameter("uPass"); if(uName.equals("w3adda") && uPass.equals("123456")){ RequestDispatcher rd=request.getRequestDispatcher("Home"); rd.forward(request, response); } else{ out.print("Wrong User Name and Password!"); RequestDispatcher rd=request.getRequestDispatcher("/index.html"); rd.include(request, response); } } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Home.java :-
if the username and password is correct so we will get this servlet file. Check below code for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Home extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uName=request.getParameter("uName"); out.print("Welcome to W3ADDA.COM "+uName); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
web.xml :-
check below xml file , you will find welcome file, servlet deployment etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> </servlet> <servlet> <servlet-name>Login</servlet-name> <servlet-class>Login</servlet-class> </servlet> <servlet> <servlet-name>Home</servlet-name> <servlet-class>Home</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Home</servlet-name> <url-pattern>/Home</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> |
Output:-
Now let’s check the output of this sample project. Check All the images of this code. You can also try this.
1. This will be the case in which you entered wrong username and password.
2. After entering correct username and password the output will be like below image.