logo

RequestDispatcher In Servlet


Show

The RequestDispatcher interface specified the capability of dispatching the request to any other useful resource, be it html, servlet or jsp. This interface also can be made used to consist of the content material of any other useful resource also. It is one of the methods of servlet collaboration.

There are two ways described withinside the RequestDispatcher interface.

Techniques for RequestDispatcher interface

The RequestDispatcher interface gives two ways. They are:

  • public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Forwards a solicitation from a servlet to another asset (servlet, JSP document, or HTML record) on the worker.
  • public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Includes the substance of an asset (servlet, JSP page, or HTML document) in the reaction.

As you can clearly see in the above given image, the reply of the second servlet is incorporated inside the reply of the first Servlet which is being sent to the client.

How to get the object of RequestDispatcher?

The getRequestDispatcher() method of ServletRequest interface comes back to the object of RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method

public RequestDispatcher getRequestDispatcher(String resource);

Here is the example of getrequestDispatcher method:

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

Example of RequestDispatcher interface:

In this example, we're validating the password entered with the aid of using the user. If the password is servlet, it'll forward the request to the WelcomeServlet, in any other case will display a blunders message: sorry username or password blunders!. In this program, we're checking for hard coded data. But you could take a look at it to the database additionally that we can see withinside the improvement chapter. In this example, we've got created following files:

  • index.html report: for purchasing enter from the user.
  • Login.java report: a servlet elegance for processing the response. If the password is servet, it'll forward the request to the welcome servlet.
  • WelcomeServlet.java report: a servlet elegance for showing the welcome message.
  • web.xml report: a deployment descriptor report that consists of the data approximately the servlet.

Index.html

<form action="servlet1" method="post">  
Name:<input type="text" name="userName"/><br/>  
Password:<input type="password" name="userPass"/><br/>  
<input type="submit" value="login"/>  
</form>

Login.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;   

public class Login extends HttpServlet {    
public void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {    

    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();           

    String n=request.getParameter("userName");  
    String p=request.getParameter("userPass");        

    if(p.equals("servlet"){  
        RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
        rd.forward(request, response);  
    }  

    else{  
        out.print("Sorry UserName or Password Error!");  
        RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
        rd.include(request, response);                      
        }  

    }  

}  

WelcomeServlet.java

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 n=request.getParameter("userName");  
    out.print("Welcome "+n);  
    }    
}  

web.xml


<web-app>  
<servlet>  
    <servlet-name>Login</servlet-name>  
    <servlet-class>Login</servlet-class>  
  </servlet>  
  <servlet>  
    <servlet-name>WelcomeServlet</servlet-name>  
    <servlet-class>WelcomeServlet</servlet-class>  
  </servlet>  

  <servlet-mapping>  
    <servlet-name>Login</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  
  <servlet-mapping>  
    <servlet-name>WelcomeServlet</servlet-name>  
    <url-pattern>/servlet2</url-pattern>  
  </servlet-mapping>  

  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
  </welcome-file-list>  
</web-app>