RequestDispatcher interface in Servlet

The RequestDispatcher interface present in javax.servlet.* package which is used to dispatch the request to another resource. The resource may be html, Servlet or jsp. This interface can also be used to include the content of another resource also. Actually this is Servlet Collaboration. There are two methods defined in the RequestDispatcher interface, such as forward() method and include() method.

public void forward(ServletRequest request, ServletResponse response)throws ServletException,java.io.IOException:

this forward() method takes two arguments such as request object and response object and throws ServletException and IOException.

These forward() method and include() method is invoked by the RequestDispatcher object. So here the question arises how we will get the RequestDispatcher object. The answer is very simple. There is a method getRequestDispatcher() present in ServletRequest interface when invoked by the request object then it returns RequestDispatcher object. RequestDispatcher rd=request.getRequestDispatcher("MyServlet2");

Let's see an example for better clarity:

In this example, we are validating a password entered by the user. If password is javarace, it will forward the request to the MyServlet2, otherwise it will show an error message: invalid password... In this example, we have considered following files:

 

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h2><u>Login Form:</u></h2><br><br>
<form action="First" method="get">
<table>
<tr><th>Username:</th><td><input type="text" name="uname">
<tr><th>Password:</th><td><input type="password" name="pass">
<tr><th><input type="submit" value="Submit"></th><td><input type="reset" value="Reset">
</table>
</form>

</body>
</html>

First.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

 

public class First extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");

PrintWriter out=response.getWriter();

String s1=request.getParameter("uname");
String s2=request.getParameter("pass");

if(s2.equals("1234"))
{
RequestDispatcher rd=request.getRequestDispatcher("Welcome");
rd.forward(request,response);
}
else
{
out.println("Invalid Password");
RequestDispatcher rd=request.getRequestDispatcher("login.html");
rd.include(request, response);
}
}
}

Welcome.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

 

public class Welcome extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println("Successfully Logged In");
}
}

web.xml

<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>java8s.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>xyz</servlet-name>
<servlet-class>java8s.Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xyz</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>


  • The forward() method is used to forward a request from a Servlet to another resource(resource may be html, jsp file or Servlet) on the server.
  • The include() method is used to include the contents of a resource in the response. public void include(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException: here also this method takes request object and response object and throws ServletException, IOException.
    1. login.html: for getting input from the user.
    2. First.java: a Servlet class for processing the response. If password is javarace, it will forward the request to the welcome Servlet.
    3. Welcome.java file: a Servlet class for displaying the welcome message.
    4. web.xml file: a deployment descriptor file that contains the information about the Servlet.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment