Chapter 5. URL Actions

   

Topics in This Chapter

  • Overview

  • The <c:import> Action

  • The <c:redirect> Action

  • The <c:url> Action

  • The <c:param> Action

  • Accessing External Resources

  • Accessing Resources in Foreign Contexts

  • Redirecting a Response

If you've developed Web applications with JavaServer Pages (JSP), you have probably found many uses for <jsp:include> and <jsp:forward>. The former includes the contents of a resource and the latter forwards control to a Web component, such as a servlet or another JSP page. On the other hand, you may have found that those actions have limited capabilities; for example, the URLs that you specify for those actions must be relative URLs, so you cannot use them to access URLs outside your Web application. JSTL provides a set of URL actions that augment the capabilities provided by <jsp:include> and <jsp:forward>; those actions are the subject of this chapter.

Before we discuss the JSTL URL actions, let's review some Web application basics and define a few terms used throughout this chapter. Java-based Web applications are stored in a directory on your filesystem; for example, Figure 5-1 illustrates a Web application that resides under the C:\ core -jstl\webapp directory.

Figure 5-1. A Simple Java-Based Web Application

graphics/05fig01.jpg

Java-based Web applications reside in a directory, but they are defined by a context; for example, the Web application depicted in Figure 5-1 could be defined in Tomcat's configuration file with a Context element, like this: [1]

[1] Different JSP containers use different terms for context paths; for example, Resin calls them web-app ids.

 <  Context path  ="/core-jstl"        docBase="C:/core-jstl/webapp"/> 

The path attribute of the Context element defines a URL that you use to access a Web application that resides in a directory specified by the docBase attribute; for example, to access the Web application shown in Figure 5-1 you would use the URL $SCHEME$HOSTNAME/core-jstl , where $SCHEME$HOSTNAME represents a scheme and a host name . For example, if the scheme is http:// and the host name is localhost , the URL for the Web application defined above would be http://localhost/core-jstl.

As websites grow, it is not uncommon for them to contain more than one Web application. From the perspective of a single Web application, the other Web applications in the same website are referred to as foreign contexts. For example, if your website has a registration Web application and a shopping application, the registration application is a foreign context relative to the shopping application, and vice versa.

When you access resources with <jsp:include>, you can specify either a context-relative path or a page-relative path; the former is relative to the top-level directory in a context (a Web application), and the latter is relative to the JSP page in which the <jsp:include> action resides.

Context-relative paths always start with a forward slash, whereas page-relative paths do not. For example, for the application shown in Figure 5-1, you can:

  • Access test_2.jsp from test_1.jsp

    • with a context-relative path, like this:

      <jsp:include page=' /jsp /test_2.jsp'/>

    • or with a page-relative path, like this:

      <jsp:include page=' jsp /test_2.jsp'/> .

  • Access test_1.jsp from test_2.jsp

    • with a context-relative path, like this:

      <jsp:include page=' /test_1 .jsp'/>

    • or with a page-relative path, like this:

      <jsp:include page=' ../test_1 .jsp'/> .

Now that we have established some common vocabulary, let's take a look at the JSTL URL actions.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net