JSTL provides URL actions that let you import content from absolute and relative URLs in addition to resources from foreign contexts. You can also redirect HTTP responses and create URLs with automatic URL rewriting and request parameter encoding, as necessary. The JSTL URL actions are listed in Table 11.12. Table 11.12. URL Actions
The <c:import> and <c:redirect> actions import content from URLs and redirect HTTP responses, respectively. Those actions provide the main functionality of the JSTL URL actions. The <c:url> and <c:param> actions provide a support role by creating URLs with URL rewriting incorporated as necessary and by encoding request parameters, respectively. The JSTL URL actions do not expose any classes or interfaces.
Syntax: [9]
Syntax #1: The content of the specified URL is sent to the current JspWriter or stored as a string in a scoped variable <c:import url [context] [var] [scope] [charEncoding]> optional <c:param> actions </c:import> Syntax #2: The content of the specified URL is only available within the body of the action through a reader <c:import url varReader [context] [charEncoding]> body content that presumably extracts information from varReader </c:import> Description: The <c:import> action is similar to the <jsp:include> action, but it offers more features and flexibility. The <c:import> action can perform the following functions:
The <jsp:include> action can only perform the first function listed above. Attributes:
Constraints and Error Handling:
In a Nutshell: The <c:import> action provides the most features of any of the JSTL URL actions and is the most heavily used. By default, <c:import> writes its content to the current JspWriter , but if you specify the var attribute (and optionally the scope attribute), <c:import> will store its imported content in a string instead. You can access that string through a scoped variable whose name corresponds to the value that you specified for the var attribute. Besides storing imported content in a string, you can also access that content directly with a reader whose name you specify with the varReader attribute. Accessing imported content with a reader is more efficient than storing it in a string, because the content is not buffered, so you may opt for the reader option when importing a resource that has a lot of content. Readers created by <c:import> are only available within the body of the <c:import> action because the <c:import> end tag is responsible for closing the reader. Because of that requirement, the reader must be available immediately after the <c:import> start tag; therefore, you cannot specify <c:param> actions in the body of the <c:import> action that was specified with a varReader attribute, as you can when content is imported directly or stored in a string. If you specify a relative URL with the url attribute that points to a resource in the same context (Web application), <c:import> imports content in exactly the same manner as does <jsp:include>. In that case, you can specify a context-relative path, which starts with a forward slash and specifies a path to a resource from the application's top-level directory, or you can specify a page-relative path that does not begin with a forward slash and that specifies a path relative to the JSP page in which the <c:import> action resides. When you import content from a relative URL in the same context, the entire environment of the importing JSP page is available to the imported resource, including request attributes, session attributes, and request parameters of the importing page. You can import resources from a foreign context by specifying the url and context attributes. In that case, the url attribute's value must be a context-relative path and the context attribute must be the context of the foreign context. Both of those attributes must start with a forward slash. When you import the content of a resource in a foreign context, only the request environment of the importing page is available to that resource. Note that not all JSP containers support accessing resources that reside in foreign contexts; if that is the case for your JSP container, you can use an absolute URL to access those resources. Besides importing resources in the same context and resources in a foreign context, <c:import> can also import content from resources specified with absolute URLs. If you specify an absolute URL for the url attribute, none of the execution environment of the importing JSP page is available to that resource for security reasons, even if that absolute URL resolves to a resource in the same context. Finally, you can specify a character encoding, for example Shift_JIS or UTF-8, that <c:import> uses to decode characters from the imported resource. You specify that encoding with the charEncoding attribute.
Syntax: [12]
Syntax #1: Without a body <c:redirect url [context]/> Syntax #2: With a body that specifies parameters <c:redirect url [context]> <c:param> actions </c:redirect> Description: The <c:redirect> action redirects an HTTP response to a specified URL and aborts processing of the JSP page in which the <c:redirect> action resides. Attributes:
In a Nutshell: The url attribute must specify a relative URL or an absolute URL. If the URL points to a resource in a foreign context, you must specify the context attribute in addition to the url attribute, and the values for both of those attributes must start with a forward slash. Like <c:import>, <c:redirect> will rewrite the URL to maintain a session, as appropriate, when you redirect to a relative resource.
Syntax: [13]
Syntax #1: Without a body <c:url value [context] [var] [scope]/> Syntax #2: With a body that specifies parameters <c:url value [context] [var] [scope]> <c:param> actions </c:url> Description: The <c:url> action processes a URL and rewrites relative URLs to maintain session information, as appropriate. Attributes:
In a Nutshell: You specify a URL with the <c:url> action's value attribute (and the context attribute if the URL points to a resource in a foreign context); <c:url> modifies that URL so that it's suitable for submission to a browser. You can also specify request parameters with nested <c:param> actions. The <c:url> action will apply URL rewriting, if necessary, to maintain session information for relative URLs only; for security reasons, <c:url> will not apply URL rewriting to absolute URLs. You can specify a page-relative URL, context-relative URL, or an absolute URL for the value attribute. You can also specify a URL that points to a resource in a foreign context by specifying both the value and context attributes. If you specify a URL that points to a resource in a foreign context, the value that you specify for the value attribute must be a context-relative URL and the value that you specify for the context attribute must begin with a forward slash. If you specify <c:param> actions inside the body of a <c:url> action, the request parameters that you specify with those <c:param> actions will be properly encoded; however, if the original URL that you specify with the value attribute contains characters, such as spaces, which should be encoded, you must make sure that they are encoded to begin with. By default, the <c:url> action writes the processed URL to the current JspWriter , but <c:url> will store its URL in a scoped variable if you specify the var attribute (and optionally, the scope attribute). The name of that scoped variable is the value that you specify for the var attribute. The <c:url> action prepends the context path of the current Web application to relative URLs that you specify with the value attribute. Because the <c:import> action also prepends the context path to relative URLs, you must not use the URL created by <c:url> to specify a URL for <c:import> for relative URLs. See "The <c:url> Action" on page 208 for more information about this restriction.
Syntax: Syntax #1: Without a body, specifying a value with the value attribute <c:param name value/> Syntax #2: With a body, specifying a value in the body of the action <c:param name> value </c:param> Description: The <c:param> action encodes a request parameter that you specify with the name and value attributes. That encoded request parameter is added to a URL created by <c:import>, <c:url>, or <c:redirect>. Attributes:
Constraints and Error Handling:
In a Nutshell: All <c:param> actions must be nested in <c:import>, <c:url>, or <c:redirect> actions. The <c:param> action is analogous to <jsp:param>, which specifies parameters for the <jsp:include> action. |