1.0          Other languages

3.3.3 Dispatcher forward


When one request been done, and need to do further work on this request by using existing request dispatcher, we can use "forward".

For example, after we add a product to shopping cart, we want to show the shopping cart.

public class AddItemToCartDispatcher extends QueryDispatcher {

    public boolean process(HttpServletRequest request, WebAppContext webContext, Controller controller)
            throws Exception {

        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute(Cart.KEY_IN_SESSION);
        if (cart == null) {
            cart = new Cart();
        String productItemId = request.getParameter("item_id");
        if (cart.containsItem(productItemId)) {
            return forward(ViewCartDispatcher.class, request, webContext, controller);

If the forward target dispatcher need some data in request of session, we need to prepare it before forward.

Normally we use "forward" only in query request without validation. As for request with validation, we don't need to forward in case of failure. Even for success, we can use pop-up success page to redirect main page to the target page.

It's not nessasory to use "forward" in case of select different HTML page as view. We can call setHtmlTemplateFileName() instead.

        if (success) {
            webContext.put(controller.getDispatchSuccessStaySecondsTag(), "2");
        } else {