RequestDispatcher in java
We are going to describe RequestDispatcher in java. RequestDispatcher is an interface that transfers the control from current web resource to another web resource (such as a Servlet, HTML, JSP) on the server. RequestDispatcher interface is define javax.servlet package. RequstDispatcher can be get using getRequestDispacther method of ServletRequest and/or from the ServletContext. Retrieved RequestDispatcher object is used as a wrapper around a server resource located at a particular path or given by a particular name.
It has two methods .
- Include() method.
- forward() method.
forward() method:-
- The forward() method of Request Dispatcher is used to transfer the request between server resources,(Servlet to Servlet, HTM and JSP).
- Servlet 1 gets the request from client and generates a response, which is not shown to user. The servlet 1 then transfer the request to servlet whose response is then sent back to client.
- Any code written after forward(request, response) method will not execute as the request is already forwarded.
- There is no record of the response of first servlet. This method is faster as compared to using sendRedirect because no network round trip to the server and back is required.
Syntax:-
RequestDispatcher req=context.getRequestDispatcher("index.jsp"); req.forward(request,response);
Forward method is explained through the following diagram:-
Example of Forward() method
ForwardRequestDispatcher
import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ForwaredRequestDispatcher extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); ServletContext context=getServletContext(); RequestDispatcher requestDispatcher=context.getRequestDispatcher("/index.html"); requestDispatcher.forward(request, response); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>RequestDispatcher</display-name> <servlet> <description></description> <display-name>RequestDispatcherExample</display-name> <servlet-name>RequestDispatcherExample</servlet-name> <servlet-class>RequestDispatcherExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>RequestDispatcherExample</servlet-name> <url-pattern>/RequestDispatcherExample</url-pattern> </servlet-mapping> </web-app>
index.html
<html> <head> <title>Home Page</title> </head> <body> <h2 align="center">Welcome</h2> <h3>This is your home page.</h3> </body> </html>
Displays Page through forward method of Request Dispatcher
- include() Method:-
- The include() method of RequestDispatcher includes the content of a resource either a servlet, or a JSP page, or an HTML file in the response.
- The servlet 1 receives the request from client. The response of servlet 2 is included without going at that page and gives the final response back to client in the same page.
- This method can be called at any time. include() can only use ServletOutputStream or Writer of the response object to write the information.
Syntax of include() method
RequestDispatcher re=request.getRequestDispatcher(?index.html?); re.include(request,response);
Include() method is explained through the following diagram:-
Example of Include() method
package rose; 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 MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(""); out.println(""); out.println("currently in the MyServlet "); RequestDispatcher rd = request.getRequestDispatcher("/index.jsp"); rd.include(request, response); out.println("back to the MyServlet "); out.println(""); out.println(""); } }
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>index.jsp</title> </head> <body> <h3> Message from the index.jsp </h3> </body> </html>
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>rose.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>MyServlet</welcome-file> </welcome-file-list> </web-app>