It means communication between servlets of a web application
Request Dispatcher:An object of the javax.servlet.RequestDispatcher interface that allows inter-servlet communication.
Object is used to include ot forward the content of another servlet.
To get RequestDispatcher Object:
//for relative path ServletContext.getRequestDispatcher(String resource) //for context path ServletRequest.getRequestDispatcher(String resource)
RequestDispatcher interface provides two methods:
public void forward(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException:
Forwards a request from a servlet to another resource on the server.
public void include(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException:
Includes the content of a resource in the response.
Examples:
RequestDispatcher rd; rd = request.getRequestDispatcher("abc.jsp?user=ank"); rd.include(request, response);
Or
RequestDispatcher dispatcher = req.getRequestDispatcher("/index.html"); dispatcher.forward(req, res);
Send Redirect can be used to communicate between two servlet present in different servers, the output will be same as request dispatcher forward example but the url of the page will be changed to redirected page
Example:
response.sendRedirect ("http://localhost:8080/Test/ServletTwo");
In this example, we are validating the password entered by the user. If password is correct, it will forward the request to the HomeServlet, otherwise will forward request to login page with an error message: Either username or password is wrong.
In this example, we have created following files:
<form action="servlet1" method="post"\> Name:<input type="text" name="userName"/><br/> Password:<input type="password" name="userPass"><br/> <input type="submit" value="login"/> </form>
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Login extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("userName"); String pass=request.getParameter("userPass"); if(pass.equals("servlet"){ RequestDispatcher dispatcher=request.getRequestDispatcher("servlet2"); dispatcher.forward(request, response); } else{ out.print("Either UserName or Password is Wrong!"); RequestDispatcher dispatcher=request.getRequestDispatcher("/index.html"); dispatcher.include(request, response); } } }
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HomeServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("userName"); out.print("HomePage "+name); } }
<web-app> <servlet> <servlet-name>Login</servlet-name> <servlet-class>Login</servlet-class> </servlet> <servlet> <servlet-name>HomeServlet</servlet-name> <servlet-class>HomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet-mapping\> <servlet-name>HomeServlet</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>