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.
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access