void forward(request, response) throws ServletException, IOExceptionThe RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.This interface can also be used to include the content of another resource also. It is one of the way of servlet collaboration.
public RequestDispatcher getRequestDispatcher(String resource);Example of using getRequestDispatcher method
RequestDispatcher rd=request.getRequestDispatcher("/welcomeServlet"); //welcomeServlet is the url-pattern of the second servlet rd.forward(request, response);//method may be include or forward
<!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=ISO-8859-1"> <title>Login Page</title> </head> <body> <form method="get" action="loginServlet"> <p>Enter your info in the following text boxes</p> Enter your name <input type="text" name="text1"/><br> Enter your password <input type="password" name="text2"/><br> <input type="submit" value="submit"/> </form> </body> </html>2. LoginServlet.java file: a servlet class for processing the response. If password is servlet, it will forward the request to the welcome servlet.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginSevlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String userName=request.getParameter("userName"); String userPass=request.getParameter("userPass"); if(userPass.equals("sweety")){ RequestDispatcher rd=request.getRequestDispatcher("welcomeServlet"); rd.forward(request, response); } else{ out.print("Sorry User Name or Password Error!"); RequestDispatcher rd=request.getRequestDispatcher("/login.html"); rd.include(request, response); } } }3. WelcomeServlet.java file: a servlet class for displaying the welcome message.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class WelcomeServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String userName=request.getParameter("userName"); out.print("Welcome "+userName); } }4. web.xml file: a deployment descriptor file that contains the information about the servlet.
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>servletRequestDispatcher</display-name> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>welcomeServlet</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>welcomeServlet</servlet-name> <url-pattern>/welcomeServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>
Labels: Servlet