Sunday 2 November 2014

RequestDispatcher

As the name suggest of "RequestDispatcher" means dispatching request. Dispatching request means dispatching request from one servelet to another servlet. This means suppose if you want call another servlet within servlet then you have to dispatch the request to other servlet so, in this case you have to use RequestDispatcher.

RequestDispatcher is a interface and defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.
This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.


Method summery of this interface-
void forward(ServletRequest request, ServletResponse response)
          Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
 void include(ServletRequest request, ServletResponse response)
          Includes the content of a resource (servlet, JSP page, HTML file) in the response.

To understand methods see below diagram-


Get the RequestDispatcher Object-

For constructing a RequestDispatcher object, you can use either the ServletRequest.getRequestDispatcher() method or ServletContext.getRequestDispatcher() method.
They both do the same thing, but impose slightly different constraints on the argument path. For the former, it looks for the resource in the same webapp to which the invoking servlet belongs and the pathname specified can be relative to invoking servlet. For the latter, the pathname must begin with '/' and is interpreted relative to the root of the webapp.

Suppose you want Servlet_A to invoke Servlet_B. If they are both in the same directory, you could accomplish this by incorporating the following code fragment in either the service method or the doGet method of Servlet_A:

   RequestDispatcher dispatcher = getRequestDispatcher("Servlet_B");
   dispatcher.forward( request, response );

where request, of type HttpServletRequest, is the first parameter of the enclosing service method (or the doGet method) and response, of type HttpServletResponse, the second. You could accomplish the same by

  RequestDispatcher dispatcher=
           getServletContext().getRequestDispatcher( "/servlet/Servlet_B" );
  dispatcher.forward( request, response );

Once you get the RequestDispatcher object, you can either forward to the resource or include the resource.

Example-
If user login process is getting success then navigate to home page-
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.         throws ServletException, IOException {  
  3.   
  4.     response.setContentType("text/html");  
  5.     PrintWriter out = response.getWriter();  
  6.           
  7.     String userName=request.getParameter("userName");  
  8.     String password=request.getParameter("userPass");  
  9.           
  10.     if(authenticateUser(userName, password)){  
  11.         RequestDispatcher rd=request.getRequestDispatcher("homeServlet");  
  12.         rd.forward(request, response);  
  13.     }  
  14.     else{  
  15.         out.print("Sorry User doesn't exist");  
  16.         RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
  17.         rd.include(request, response);  
  18.                       
  19.         }  
  20.     } 

Thanks!!!!

1 comment:

  1. Java Class By Rk: Requestdispatcher >>>>> Download Now

    >>>>> Download Full

    Java Class By Rk: Requestdispatcher >>>>> Download LINK

    >>>>> Download Now

    Java Class By Rk: Requestdispatcher >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete