void forward(request, response) throws ServletException, IOException
The 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.
There are two methods defined in the RequestDispatcher interface.
The RequestDispatcher interface provides two methods. They are:
HttpSession interface
As you see in the above figure, response of second servlet is sent to the client. Response of the first servlet is not displayed to the user.
As you can see in the above figure, response of second servlet is included in the response of the first servlet that is being sent to the client.
The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher.
Syntax of getRequestDispatcher method:
public RequestDispatcher getRequestDispatcher(String resource);
RequestDispatcher rd=request.getRequestDispatcher("/welcomeServlet"); //welcomeServlet is the url-pattern of the second servlet rd.forward(request, response);//method may be include or forward
In this example, we are validating the password entered by the user. If password is servlet, it will forward the request to the WelcomeServlet, otherwise will show an error message: sorry username or password error!. In this program, we are checking for hard coded information. But you can check it to the database also that we will see in the development chapter.
1. :login.html file for getting input from the user.
<!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>
Now let us start tomcat server using C:Program Files (x86)Apache Software FoundationTomcat 7.0binstartup.bat (on windows) or /bin/startup.sh (on Linux/Solaris etc.) and finally type http://localhost:8080/login.html in browser’s address box. If everything goes fine, you would get following result:
With wrong password it request dispatched to again login form.
With right user name and password.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…