JSTL url Tag <c:url>

JSTL URL tag is used to format and save url in a variable to use it at later stage. The <c:url> tag formats a URL into a string and stores it into a variable. This tag automatically performs URL rewriting when necessary. The var attribute specifies the variable that will contain the formatted URL.

The JSTL url tag is just an alternative method of writing the call to the response.encodeURL() method. The only real advantage the url tag provides is proper URL encoding, including any parameters specified by children param tag.

JSTL <C:URL> Tag Example:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL URL Tag Example</title>
</head>
<body>
  <c:url value="https://www.dineshonjava.com" var="tutorialLink">
    <c:param name="author" value="Dinesh"></c:param>
  </c:url>
  <c:import url="${tutorialLink}"></c:import>
</body>
</html>

As you can see above, <c:url> tag is being used to store the value of url in a variable tutorialLink.

In <c:import> tag, the url attribute will have following value.

https://www.dineshonjava.com?author=Dinesh

Attributes of <c:url> tag are:

      1. value:This attribute provides URL to be processed.

       2. var: This attribute provides name of the exported scoped variable for the processed url. The type of the scoped variable is String.

       3. context: This attribute provides name of the context when specifying a relative URL resource that belongs to a foreign context.

       4. scope: This attribute provides the scope for var.

<<Previous <<   || Index ||   >>Next >>

Previous
Next