Cover | Table of Contents | Colophon
http://jakarta.apache.org/site/mail.html,
before joining. You can then join one or more of the
Jakarta Project's
mailing lists, including those for Struts, from the URL http://jakarta.apache.org/site/mail2.html.
http://jakarta.apache.org/struts/. For more
information on downloading and installing Struts, see Appendix B.
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 may 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;
/**
* Perform a redirect for the page "redirect.jsp"
*/
public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
redirect( request, response );
}
public void doPost(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( ) );
// Put some objects into request scope
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) );
}
}
action
package contains 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 util package. While it may be confusing
at first, it's not that hard to get accustomed to
where everything is. Table 3-1 enumerates the
top-level packages and provides brief descriptions of their purposes.
A few of these top-level packages also contain subpackages, which
will be covered in later chapters.
Return to Programming Jakarta Struts