RequestDispacher is an interface that provides the facility to forward a request to another resource or include the content of another resource. RequestDispacher provides a way to call another resource from a servlet. Another resource can be servlet, jsp or html.
Methods of RequestDispacher interface:
1. forward(ServletRequest request,ServletResponse response): This method forwards a request from a servlet to another resource on the server.
Syntax:
public void forward(ServletRequest request,ServletResponse response) throws ServletException, IOException
2. include(ServletRequest request,ServletResponse response): This method includes the content of a resource in the response.
Syntax:
public void include(ServletRequest request,ServletResponse response) throws ServletException, IOException
How to get an object of RequestDispacher.
RequestDispacher object can be gets from HttpServletRequest object.ServletRequest’s getRequestDispatcher()method is used to get RequestDispatcher object.
RequestDispatcher requestDispatcher = request.getRequestDispatcher(“/another resource”);
After creating RequestDispatcher object you call forword or include method as per your requirement.
requestDispatcher.forward(request, response);
or
requestDispatcher.include(request, response);
Example:
LoginServlet.java
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; /** * This class is used for authentication process. * @author w3spoint */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor public LoginServlet() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //get parameters from request object. String userName = request.getParameter("userName").trim(); String password = request.getParameter("password").trim(); //check for null and empty values. if(userName == null || userName.equals("") || password == null || password.equals("")){ out.print("Please enter both username" + " and password. <br/><br/>"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html"); requestDispatcher.include(request, response); }//Check for valid username and password. else if(userName.equals("jai") && password.equals("1234")){ RequestDispatcher requestDispatcher = request.getRequestDispatcher("WelcomeServlet"); requestDispatcher.forward(request, response); }else{ out.print("Wrong username or password. <br/><br/>"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html"); requestDispatcher.include(request, response); } } } |
WelcomeServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This class is used to show the message * when user logged in successfully. */ public class WelcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor public WelcomeServlet() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>You are logged " + "in successfully.</h1>"); out.println("</html></body>"); } } |
login.html
<!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</title> </head> <body> <form action="LoginServlet" method="post"> Username:<input type="text" name="userName"/> <br/><br/> Password:<input type="password" name="password"/> <br/><br/> <input type="submit" value="login"/> </form> </body> </html> |
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"> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class> com.w3spoint.business.LoginServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class> com.w3spoint.business.WelcomeServlet </servlet-class> </servlet> <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> |
Output:
Enter username: jai and password:1234
Click on login button.
Download this example.
Next Topic: sendRedirect in servlet with example.
Previous Topic: load-on-startup in web.xml with example.