5.4 The <c:url> ActionThe <c:url>" on page 490 for a more complete description of <c:url> syntax.
The mandatory value attribute specifies the URL that's processed by the <c:url> action. The context attribute lets you specify a foreign context. Like <c:import> and <c:redirect>, if you specify the context attribute for <c:url>, you must also specify a context-relative URL, with the value attribute, that points to a resource in that foreign context. By default, <c:url> sends the processed URL to the current JspWriter, but you can store that URL in a scoped variable instead if you specify the var attribute and, optionally, the scope attribute.Like <c:import> and <c:redirect>, you can specify request parameters that are encoded in the URL that <c:url> processes with nested <c:param> actions. You can do that with the following syntax:
If you specify a context-relative or page-relative URL for the value attribute, <c:url> will prepend the context path of the Web application to the URL; for example, consider the following use of <c:url>:
If the context path of the Web application is /core-jstl/webapp, <c:url> will produce the following URL: /core-jstl/webapp/test_1.jsp, not taking into account possible URL rewriting. Because of this feature, you must not use <c:url> in conjunction with <c:import> or <c:redirect> for relative URLs because those actions also prepend the context path to relative URLs before passing the URL to the request dispatcher. For example, consider the following code:
The preceding code fragment is not equivalent to the following code fragment:
The preceding code fragment will throw an exception because both <c:url> and <c:import> will try to prepend the context path to the relative URL. URLs processed by <c:url> actions are meant to be sent directly to the browser; for example:
The preceding code fragment creates a URL with <c:url> and uses the resulting Accessing External Resources" on page 210 and "Accessing Resources in Foreign Contexts" on page 215 both use <c:url> to process URLs that are sent directly to the browser.
![]() |