Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

RequestDispatcher


The 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 interface

The RequestDispatcher interface provides two methods. They are:

  1. public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
  2. public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Includes the content of a resource (servlet, JSP page, or HTML file) in the response.

forward

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.

include

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.

How to get the object of RequestDispatcher

The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method

  1. public RequestDispatcher getRequestDispatcher(String resource);

Example of using getRequestDispatcher method

  1. RequestDispatcher rd=request.getRequestDispatcher(“servlet2”);
  2. //servlet2 is the url-pattern of the second servlet
  3. rd.forward(request, response);//method may be include or forward

Example of RequestDispatcher interface

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 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 file: for getting input from the user.
  • Login.java file: a servlet class for processing the response. If password is servet, it will forward the request to the welcome servlet.
  • WelcomeServlet.java file: a servlet class for displaying the welcome message.
  • web.xml file: a deployment descriptor file that contains the information about the servlet.

requestdispatcher

index.html

  1. <form action=“servlet1” method=“post”>
  2. Name:<input type=“text” name=“userName”/><br/>
  3. Password:<input type=“password” name=“userPass”/><br/>
  4. <input type=“submit” value=“login”/>
  5. </form>

Login.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class Login extends HttpServlet {
  5. public void doPost(HttpServletRequest request, HttpServletResponse response)
  6.         throws ServletException, IOException {
  7.     response.setContentType(“text/html”);
  8.     PrintWriter out = response.getWriter();
  9.     String n=request.getParameter(“userName”);
  10.     String p=request.getParameter(“userPass”);
  11.     if(p.equals(“servlet”){
  12.         RequestDispatcher rd=request.getRequestDispatcher(“servlet2”);
  13.         rd.forward(request, response);
  14.     }
  15.     else{
  16.         out.print(“Sorry UserName or Password Error!”);
  17.         RequestDispatcher rd=request.getRequestDispatcher(“/index.html”);
  18.         rd.include(request, response);
  19.         }
  20.     }
  21. }

WelcomeServlet.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class WelcomeServlet extends HttpServlet {
  5.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  6.         throws ServletException, IOException {
  7.     response.setContentType(“text/html”);
  8.     PrintWriter out = response.getWriter();
  9.     String n=request.getParameter(“userName”);
  10.     out.print(“Welcome “+n);
  11.     }
  12. }

web.xml

  1. <web-app>
  2.  <servlet>
  3.     <servlet-name>Login</servlet-name>
  4.     <servlet-class>Login</servlet-class>
  5.   </servlet>
  6.   <servlet>
  7.     <servlet-name>WelcomeServlet</servlet-name>
  8.     <servlet-class>WelcomeServlet</servlet-class>
  9.   </servlet>
  10.   <servlet-mapping>
  11.     <servlet-name>Login</servlet-name>
  12.     <url-pattern>/servlet1</url-pattern>
  13.   </servlet-mapping>
  14.   <servlet-mapping>
  15.     <servlet-name>WelcomeServlet</servlet-name>
  16.     <url-pattern>/servlet2</url-pattern>
  17.   </servlet-mapping>
  18.   <welcome-file-list>
  19.    <welcome-file>index.html</welcome-file>
  20.   </welcome-file-list>
  21. </web-app>

dispatcher1

dispatcher2

dispatcher3

dispatcher4

 

Comments

comments