Redirect Versus Forward
Back to the shopping cart example. When addtocart.jsp page has added the product to the cart, it needs to invoke the catalog.jsp page to show the user the updated cart contents.
There are two
ways you can let one
page invoke another page: redirecting or forwarding. Forwarding was
used in Example 10-2 to display an appropriate page
depending on the result of the user input validation. In Example 10-8, redirection is used to display the catalog
page after adding a new product to the cart. The
<c:redirect>
JSTL action, described in Table 10-8, sends a
redirect response to the browser with the new location defined by the
url
attribute. If URL rewriting is used for
session tracking, the URL is encoded with the session ID. If the body
of this action contains <c:param>
actions,
described in Table 10-4, each parameter is added to
the URL as query string parameters, encoded according to rules in the
HTTP specification.
Attribute name |
Java type |
Dynamic value accepted |
Description |
url |
String |
Yes |
Mandatory. An absolute URL, or a context- or page-relative path. |
context |
String |
Yes |
Optional. The context path for the application, if the resource isn’t part of the current application. |
There’s an important difference between a forward and a redirect. When you forward, the target page is invoked through an internal method call by the JSP container; the new page continues to process the same request and the browser isn’t ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.