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) { request.setAttribute("selectedScreen", request.getServletPath()); RequestDispatcher dispatcher = request. getRequestDispatcher("/template.jsp"); if (dispatcher != null) dispatcher.forward(request, response); } public void doPost(HttpServletRequest request, ... }
No comments:
Post a Comment