Java Reference
In-Depth Information
If you specify a context-relative or page-relative URL for the
value
attribute,
<c:url>
prepends
the context path of the web application to the URL; for example, if the context path of the web
application is
/bookWeb/books
, the
<c:url>
action
<c:url value='/book.jsp'/>
will result
in
url:/bookWeb/books/book.jsp
.
The <c:redirect>Action
The
<c:redirect>
action sends an HTTP redirect to the client, redirecting the browser to an alternate
URL. The
<c:redirect>
action provides the URL rewriting with a redirect.
Here's the syntax without body content:
<c:redirect url="value" [context="context"]/>
Here's the syntax with body content to specify query string parameters:
<c:redirect url="value" [context="context"]>
<c:param> subtags
</c:redirect>
The
<c:redirect>
action has two attributes: the URL that will be used to redirect to and an optional context.
The attributes of <c:redirect> are listed in the Table
3-23
.
Table 3-23.
<c:redirect> Attributes
Name
Type
Description
url
String
The URL of the resource to redirect to
context
String
The name of the context when redirecting to a relative URL resource that belongs to
a foreign context
The URL, relative or absolute, follows the same URL rewriting rules as
<c:url>
. Listing 3-55
illustrates redirecting to an external resource, which is an absolute URL.
Listing 3-55. Using <c:redirect>
<c:redirect url="
http://www.yourbookstore.com
"
/>
You can redirect to a resource in a foreign context using the
context
attribute. The URL specified
must start with a
/
as a context-relative URL, and as defined, the context name must also start with
a
/
. Listing 3-56 illustrates redirecting to a foreign resource in a foreign context.
Listing 3-56. redirecting to a foreign resource in a foreign context.
<c:redirect url="/foreignresource.html" context="/foreigncontext" />
In this chapter, we have looked at the nuts and bolts of the Java EE machine in the web tier: the web
components (servlets and JSP), the feature-rich Expression Language, and the ready-to-use custom
actions (JSTL). Now we'll talk about best-practice solutions using patterns.