The RequestDispatcher interface specified the capability of dispatching the request to any other useful resource, be it html, servlet or jsp. This interface also can be made used to consist of the content material of any other useful resource also. It is one of the methods of servlet collaboration.
There are two ways described withinside the RequestDispatcher interface.
The RequestDispatcher interface gives two ways. They are:
As you can clearly see in the above given image, the reply of the second servlet is incorporated inside the reply of the first Servlet which is being sent to the client.
The getRequestDispatcher() method of ServletRequest interface comes back to the object of RequestDispatcher. Syntax:
Syntax of getRequestDispatcher method
public RequestDispatcher getRequestDispatcher(String resource);
RequestDispatcher rd=request.getRequestDispatcher("servlet2"); //servlet2 is the url-pattern of the second servlet rd.forward(request, response);//method may be include or forward
In this example, we're validating the password entered with the aid of using the user. If the password is servlet, it'll forward the request to the WelcomeServlet, in any other case will display a blunders message: sorry username or password blunders!. In this program, we're checking for hard coded data. But you could take a look at it to the database additionally that we can see withinside the improvement chapter. In this example, we've got created following files:
Index.html
<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>
Login.java
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 n=request.getParameter("userName"); String p=request.getParameter("userPass"); if(p.equals("servlet"){ RequestDispatcher rd=request.getRequestDispatcher("servlet2"); rd.forward(request, response); } else{ out.print("Sorry UserName or Password Error!"); RequestDispatcher rd=request.getRequestDispatcher("/index.html"); rd.include(request, response); } } }
WelcomeServlet.java
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 n=request.getParameter("userName"); out.print("Welcome "+n); } }
web.xml
<web-app> <servlet> <servlet-name>Login</servlet-name> <servlet-class>Login</servlet-class> </servlet> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>