Processing Requests

When the Struts servlet receives a request, it first uses the processPath( ) method (Example 19-9) to extract the path part that is mapped to an action class. It then locates, or creates, the instance of the matching action class and calls its perform( ) method. The ActionForward instance returned by the perform( ) method is processed by the Struts servlet’s processActionForward( ) method shown in Example 19-12.

Example 19-12. Forward processing
protected void processActionForward(ActionForward forward,
    ActionMapping mapping, ActionForm formInstance,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
  
    if (forward != null) {
        String path = forward.getPath(  );
        if (forward.getRedirect(  )) {
            if (path.startsWith("/"))
                path = request.getContextPath(  ) + path;
            response.sendRedirect(response.encodeRedirectURL(path));
        } else {
            RequestDispatcher rd =
                getServletContext(  ).getRequestDispatcher(path);
            if (rd == null) {
                response.sendError(response.SC_INTERNAL_SERVER_ERROR,
                    internal.getMessage("requestDispatcher", path));
                return;
            }
            rd.forward(request, response);
        }
    }
}

This method illustrates a number of interesting things about how to pass control to another part of the application—a servlet or a JSP page—that you need to be aware of if you decide to implement your own Controller servlet.

The ActionForward argument contains all the information Struts needs to pass control to the next component. Again, this is typically a JSP page that ...

Get JavaServer Pages, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.