The RequestDispatcher Interface

Given the simplicity of servlets, it makes sense to keep each servlet focused on a specific task, and then set up multiple servlets to collaboratively achieve a more complex task. Servlets can take care of the mechanical aspects of such collaborative efforts easily by implementing the RequestDispatcher interface.

The RequestDispatcher interface provides two key capabilities:

  • forward: This method allows a servlet to forward a request to another Web component. The servlet forwarding the request may process the request in some way prior to the forwarding. Forward can effectively be used to achieve servlet chaining where each link in the chain produces some output that can be merged with the original request data, and then be used as the input to the next servlet in the chain. This is essentially similar to the concept of pipes in the UNIX world.

    Note that the term “redirect” is sometimes used interchangeably with “forward,” intending the same meaning. However, this should not be confused with the sendRedirect method found on the servlet response. A sendRedirect call does not guarantee preservation of the request data when it forwards to a new page, so it does not allow for the same servlet chaining capabilities.

  • include: This method permits the contents of another Web component to be included in the response from the calling servlet. The first servlet simply includes the other servlet at the appropriate point in the output, and the output from the servlet being included is added to the output stream. This is similar in concept to Server Side Includes (SSI).[2]

    [2] SSI allows embedding of special tags into an HTML document. The tags are understood by the Web server and are translated dynamically as the HTML document is served to the browser. JSPs build on this idea.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156