RequestDispatcher

RequestDispacther is an interface used to receive requests from the users and bind it with other files such as HTML file, Servlet file, JSP file etc. Servlet container is responsible to create RequestDispatcher object.

RequestDispacther provides forward() and include() methods. These methods are used to call RequestDispacther.

Methods of RequestDispatcher

                  Method

            Description

void forward(ServletRequest req, ServletResponse res)

If this method is invoked then the request of current file is send forward to the another file of any type such as HTML, Servlet, JSP etc. and the response of that file is provided by the server.

void include(ServletRequest req, ServletResponse res)

If this method is invoked then the content of current file such as HTML, Servlet, JSP is included with the response.

Example of RequestDispatcher interface

In this example we create a form in html file. ServletChecker.java file checks the password field of that form. If the entered password length is less than 8 then include() method is invoked and generates error else forward() method is invoked and forward that request to another java file.

index.html

<form action="serv" method="post">
<table>
<h1>
<tr><td><h3>Name:</td><td><input type="text" name="name"></h3></td></tr> <br>
<tr><td><h3>Password:</td><td><input type="password" name="pass" placeholder="Must be of 8 characters"></h3></td></tr> <br>
<tr><td><h1><input type="submit" value="sign up"></h1></td></tr>
</h1>
</table>
</form>

ServletChecker.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/serv")
public class ServletChecker extends HttpServlet
{
 public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
 res.setContentType("text/html");
 PrintWriter pw=res.getWriter();
 String st1=req.getParameter("pass");
 int i1=st1.length();
 if(i1<8)
 {
 pw.println("<h1>Error:Password must be of 8 character</h1>");
 RequestDispatcher rd=req.getRequestDispatcher("/index.html");
 rd.include(req,res);
 }
 else 
 {
 RequestDispatcher rd1=req.getRequestDispatcher("ServletForward");
 rd1.forward(req,res);
 }
 }
}

ServletForward.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/ServletForward")
public class ServletForward extends HttpServlet
{
 public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 res.setContentType("text/html");
 PrintWriter pw=res.getWriter();
 String st1=req.getParameter("name");
 pw.println("<h1>Welcome "+ st1+"</h2>");
 pw.println("<h3>You registered successfully</h3>");
 }}
Output: Now we have to fill up this form. The inserted length of the password is less than 8 character. So it will generate error message and include the current form within it. As expected error message generated. Now inserted length of the form is greater than 8 character. So forward() method is invoked and sent as a request to another page. The following response generated.