URL operation
JSTLcore
The remaining tags in the library are mainly about URLs. The first of these tags is named<c:url>
Generate a URL. Especially,<c:url>
Three functional elements are provided, which are particularly useful when constructing URLs for J2EE Web applications:
- Append the name of the current servlet context.
- Rewrite URL for session management
- URL encoding of the Request Parameter Name and Value
Listing 12 shows<c:url>
Mark syntax.value
Attribute is used to specify the basic URL, and then mark the conversion as necessary. If this basic URL starts with a slash, the servlet context name will be added before it. Availablecontext
Attribute provides an explicit context name. If this attribute is omitted, the name of the current servlet context is used. This is especially useful because the servlet context name is determined during deployment rather than during development. (If this basic URL does not start with a slash, it is considered as a relative URL, and the context name does not need to be added .)
Listing 12. <C: URL> operation syntax
<c:url value=" expression" context=" expression" var=" name" scope=" scope"> <c:param name=" expression" value=" expression"/> ...</c:url>
|
URL rewriting is caused<c:url>
The operation is automatically executed. If the JSP Container detects a cookie that stores the user's current session ID, it does not have to be overwritten. However, if such a cookie does not exist<c:url>
All generated URLs are overwritten to encode the session ID. Note: if an appropriate cookie exists in the subsequent request<c:url>
The URL will be stopped to include this identifier.
Ifvar
The attribute provides a value (orscope
Attribute provides a corresponding value, which is optional), then the generated URL is assigned to the specified variable with a limited scope. Otherwise, the currentJspWriter
Output the generated URL. This capability allows direct output of results<c:url>
Mark as a value, for example, as HTML<a>
Markedhref
The attribute value, as shown in listing 13.
Listing 13. Generating URL as HTML Tag attribute values
<a href="<c:url value='/content/sitemap.jsp'/>">View sitemap</a>
|
Finally, if<c:param>
If any request parameter is specified, their names and values are added to the generated URL using the standard notation of the http get request. URL encoding: To generate a valid URL, any characters in the names or values of these parameters are converted as appropriate. Listing 14 demonstrates<c:url>
.
Listing 14. Generating a URL with Request Parameters
<c:url value="/content/search.jsp"> <c:param name="keyword" value="${searchTerm}"/> <c:param name="month" value="02/2003"/></c:url>
|
The JSP code in listing 14 is deployed toblog
Servlet context, which limits the scope of VariablessearchTerm
Is set"core library"
. If the session cookie is detected, the URL generated in listing 14 is similar to the URL in listing 15. Note: add the context name before and append the request parameters. In addition,keyword
Space andmonth
The slashes in the parameter values are encoded according to the http get parameter (specifically, spaces are converted+
And the slash is converted%2F
Sequence ).
Listing 15. url generated when session cookies exist
/blog/content/search.jsp?keyword=foo+bar&month=02%2F2003
|
If no session cookie exists, the generated result is shown in listing 16. Similarly, the servlet context is added to the front, and the URL encoding request parameters are appended to the back. However, the basic URL is rewritten to contain the specified session ID. When a browser sends a URL request in this way, the JSP Container automatically extracts the session ID and associates the request with the corresponding session. In this way, J2EE applications requiring session management do not need to rely on cookies enabled by application users.
Listing 16. url generated when no session cookie exists
/blog/content/search.jsp;jsessionid=233379C7CD2D0ED2E9F3963906DB4290 ?keyword=foo+bar&month=02%2F2003
|