wellnwill

RequestDispatcher

RequestDispatcher

  • RequestDispatcher is and interface in javax.servlet package
  • It is used to dispatch the request from one web component to other using forward() method
  • It can also include other web component in Servlet like HTML, JSP using include() method
  • RequestDispatcher object can be obtained using request.getRequestDispatche()
  • RequestDispatcher rd = request.getRequestDispatcher(path + page);
  • RequestDispatcher has two methods
    • public void forward(ServletRequest req, ServletResponse res)
    • public void include(ServletRequest req, ServletResponse res)

The forward() method

  • This method forward requests from
    • Servlet to HMLT similarly JSP to HTML
    • Servlet to JSP similarly JSP to JSP
    • Servlet to Servlet similarly JSP to Servlet
  • It is used to provide programmatically access to resources placed inside WEB-INF
  • Once servlet can have multiple forward() placed in conditional blocked such that only one will be executed otherwise java.lang.IllegalStateException will be thrown
  • After forwarding statement servlet can have following statements to execute but those statements cannot write any content to response stream

Syntax

RequestDispatcher rd = request.getRequestDispatcher(path + page);
rd.forward(request, response);

Example

A typical example of using RequestDispatcher.forward in login scenario where if login is successful, forward the request to home page otherwise forward it to error page. Below is the code snippet for the same:

protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
  String userName = request.getParameter("userName");
  String password = request.getParameter("password");

  if (userName.equals(password)) {
   page = "home.html";
  } else {
   page = "error.html";
  }

  RequestDispatcher rd = request.getRequestDispatcher(path + page);
  rd.forward(request, response);
 }

The include() method

  • Used to include other resources like servlet and/or JSP in response
  • Unlike forward(), servlet can have many include() statements and all will be executed

Syntax

RequestDispatcher rd1 = request.getRequestDispatcher(path+"header.html");
rd1.include(request, response);

Example

A typical example of using include() method is to include header and footer in the web page and may be menu section in the page.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String page = request.getParameter("page");
  
  RequestDispatcher rd1 = request.getRequestDispatcher(path+"header.html");
  rd1.include(request, response);
  
  RequestDispatcher rd2 = request.getRequestDispatcher(path+page+".html");
  rd2.include(request, response);
  
  RequestDispatcher rd3 = request.getRequestDispatcher(path+"footer.html");
  rd3.include(request, response);
 }

The sendRedirect

  • Redirects the control to another web component
  • This includes browser refresh or url change

Syntax

response.sendRedirect("index.html");

Example

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException {
  String enteredURL = request.getParameter("url");

  if (enteredURL == null || enteredURL.length() == 0) {
   RequestDispatcher rd = request.getRequestDispatcher("error.html");
   rd.forward(request, response);
  } else {
   response.sendRedirect("index.html");
  }
 }

Difference betweenRequestDispatcher.forward and HttpServletResponse.sendRedirect

 
Footer with Map