Thursday, 15 December 2011

Transferring Control to Another Web Component

In some applications, you might want to have one Web component do preliminary processing of a request and have another component generate the response. For example, you might want to partially process a request and then transfer to another component depending on the nature of the request.
To transfer control to another Web component, you invoke the forward method of a RequestDispatcher. When a request is forwarded, the request URL is set to the path of the forwarded page. If the original URL is required for any processing, you can save it as a request attribute. The Dispatcher servlet, used by a version of the Duke's Bookstore application described in the section A Template Tag Library, saves the path information from the original URL, retrieves a RequestDispatcher from the request, and then forwards to the JSP page template.jsp.
public class Dispatcher extends HttpServlet {
   public void doGet(HttpServletRequest request, 
      HttpServletResponse response) {
      RequestDispatcher dispatcher = request.
      if (dispatcher != null)
         dispatcher.forward(request, response);
   public void doPost(HttpServletRequest request, 
The forward method should be used to give another resource responsibility for replying to the user. If you have already accessed a ServletOutputStream or PrintWriter object within the servlet, you cannot use this method; it throws an IllegalStateException.

No comments:

Post a Comment