Jstl url tag

Source: Internet
Author: User

URL operation

JSTLcoreThe 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.valueAttribute 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. AvailablecontextAttribute 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.

IfvarThe attribute provides a value (orscopeAttribute provides a corresponding value, which is optional), then the generated URL is assigned to the specified variable with a limited scope. Otherwise, the currentJspWriterOutput the generated URL. This capability allows direct output of results<c:url>Mark as a value, for example, as HTML<a>MarkedhrefThe 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 toblogServlet context, which limits the scope of VariablessearchTermIs 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,keywordSpace andmonthThe slashes in the parameter values are encoded according to the http get parameter (specifically, spaces are converted+And the slash is converted%2FSequence ).

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.