By Chuck Cavaness
Price: $44.95 USD
£31.95 GBP
Cover | Table of Contents | Colophon
javax.servlet.http.HttpServletRequest
interface. Among other things, this object contains a collection of
key/value attribute pairs that can be used to store objects for the
lifetime of the request. The key of each pair is a
String, and the value can be any type of
Object. The methods to store objects in and
retrieve them from the request scope are:public void setAttribute( String name, Object obj ); public Object getAttribute( String name );
removeAttribute() method; however,
because the scope of the attribute is only for the lifetime of the
request, it is not as important to remove them as it is for other
scoped attributes. Once the server fulfills a request and a response
is returned to the client, the request and its attributes are no
longer available to the client and can be garbage-collected by the
JVM.HttpServletRequest
object from the URI query string and
data that is sent in a POST method. The parameters are formatted as
key/value pairs and are accessed by applications as request
parameters. URL parameters play an important role in all web
applications, and the Struts framework is no exception.sendRedirect() method
is invoked, it causes the web container to return to the browser a
response indicating that a new URL should be requested. Because the
browser issues a completely new request, any objects that are stored
as request attributes before the redirect occurs will be lost. This
is one of the biggest differences between a forward and redirect.
Figure 2-5 illustrates why this occurs.
package com.oreilly.struts.chapter2examples;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
public class RedirectServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
redirect(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
redirect(request, response);
}
/**
* Set a few URL parameters and objects for the request to see what happens
* to them during a redirect.
*/
protected void redirect(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
log("A request arrived for " + req.getServletPath( ));
req.setAttribute("firstName", "John");
req.setAttribute("lastName", "Doe");
String contextPath = req.getContextPath( );
String redirectStr = contextPath + "/result.jsp?username=foo&password=bar";
log("redirecting to " + redirectStr);
// Always call the encodeRedirectURL method when perfoming a redirect
resp.sendRedirect(resp.encodeRedirectURL(redirectStr));
}
}
validator package
shown in Figure 3-4 does not represent the entire
set of classes and interfaces necessary for the
Validator framework. These are only
the Struts-specific extensions necessary to use the Validator
framework with Struts. There is also a ninth top-level package named
config, which consists of a single Java class.
We'll ignore this package for now;
it's not relevant to the discussion in this chapter.action
package contains some classes for the controller, some that are used
by the view domain, and even a few that probably would have been
better off in the
org.apache.struts.action.ActionServlet
class.ActionServlet extends the
javax.servlet.http.HttpServlet
class and is responsible for packaging and routing HTTP traffic to
the appropriate handler in the framework. The
ActionServlet
class is not abstract and therefore can be used as a concrete
controller by your applications. Prior to Version 1.1 of the Struts
framework, the ActionServlet was solely
responsible for receiving the request and processing it by calling
the appropriate handler. In Version 1.1, a new class, called
org.apache.struts.action.RequestProcessor
, was introduced to process the request
for the controller. The main reason for decoupling the request
processing from the ActionServlet is to provide
you with the flexibility to subclass the
RequestProcessor with your own version and modify
how the request is processed.ActionFormsActionForm objects are used in the
framework to pass client input data back and forth between the user
and the business layer. The framework automatically collects the
input from the request and passes this data to an
Action using a form bean, which then can be passed
along to the business layer. To keep the presentation layer decoupled
from the business layer, you should not pass the
ActionForm itself to the business layer; rather,
create the appropriate DTO using the data from the
ActionForm. The following steps illustrate how the
framework processes an
ActionForm for every request:ActionForm has been configured.ActionForm is configured for the action, use
the name attribute from the
action element to look up the form bean
configuration information.ActionForm
already has been created.ActionForm instance is present in the
appropriate scope and it's the same type as needed
for the new request, reuse it.ActionForm and store it in the appropriate scope
(set by the scope attribute for the
action element).ActionServlet,
RequestProcessor, and Action
classes provide the controller components; the
Action communicates with your
application's model components; and the combination
of the ActionForm, DTOs, JSP pages, and tag
libraries make up the view.
http://www.somehost.com/storefront/index.jsp
into his browser's location bar, the JSP page will
be served from the root of the Storefront web application.