From within a servlet, you can direct the control process to a destination resource by using the forward method of the Javax.servlet.RequestDispatcher class. In the action class of the login application, the code is in the following form:
RequestDispatcher rd = request.getrequestdispatcher (destination);
Rd.forward (request, response);
Where destination is the path to a destination resource.
But in a typical struts application, you can use the Actionforward class as an alternative. The advantage of using this class is that you no longer need to create a RequestDispatcher object and invoke its forward method.
You can use the Actionforward class in the Execute method of an action class. Note that one of the overloaded Execute methods has the following definition, which returns a Actionforward object:
Public Actionforward Execute (
Actionmapping Mapping,
Actionform form, HttpServletRequest request,
HttpServletResponse response)
Throws Exception
Because we hadn't talked about the Actionforward class at the time, the Execute method for all the action classes in the first and second parts of the series returned only null values. Now, in the Execute method of an action class, you can replace the following RequestDispatcher object instance with the Actionforward class:
RequestDispatcher rd = request.getrequestdispatcher (destination);
Rd.forward (request, response);
The new code becomes: Return (new Actionforward (destination));
Building Actionforward Objects
The Actionforward class provides the following five types of constructors:
Public Actionforward ()
Public Actionforward (String path)
Public Actionforward (String path, Boolean redirect)
Public Actionforward (string name, String path, Boolean redirect)
Public Actionforward (string name, String path, Boolean redirect, Boolean contextrelative)
Although these constructors are not required, we should be aware of the following points. In these constructors, the second is probably the most common. The path parameters in the four constructors represent the paths to the destination resource. The redirect Boolean value in the last three constructors indicates whether a redirect (redirect) has been performed. (This value is set to False by default because redirect is slower than forward.) Finally, the Contextrelative Boolean value in the fifth constructor indicates whether the path should be context-relative, not module-relative.
Similarly, a Actionforward instance can have a logical name that you can use to find an instance associated with a particular Actionmapping object. (See part IV of this series about actionmapping.) )