91-9990449935 0120-4256464 |
RequestDispatcher in ServletThe RequestDispatcher 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. Methods of RequestDispatcher interfaceThe RequestDispatcher interface provides two methods. They are:
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.
How to get the object of RequestDispatcherThe getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher. Syntax: Syntax of getRequestDispatcher methodExample of using getRequestDispatcher methodExample of RequestDispatcher interfaceIn 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 cheking for hardcoded information. But you can check it to the database also that we will see in the development chapter. In this example, we have created following files:
index.html Login.java WelcomeServlet.java web.xml
download this example
download this example (developed in Myeclipse IDE) download this example (developed in eclipse IDE) download this example (developed in netbeans IDE)
Next TopicSendRedirect() Method
|