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.
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.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 .
forward() method:-
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
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>
Download Source Code in Forward method
Download Source Code in Include method
Ads