<c:url> Tag
The <c:url> tag formats a URL into a string and stores it in a variable.
This tag automatically rewrites the URL when necessary.
The var attribute is used to store the formatted URL.
The <c:url> tag is an alternative method to calling the response.encodeURL() method. Its real advantage lies in providing proper URL encoding, including parameters specified in <c:param>.
Syntax
<c:url
var="<string>"
scope="<string>"
value="<string>"
context="<string>"/>
Attributes
The <c:url> tag has the following attributes:
| Attribute | Description | Required | Default Value |
|---|---|---|---|
| value | Base URL | Yes | None |
| context | Name of the local web application | No | Current application |
| var | Variable name representing the URL | No | Print to page |
| scope | Scope of the var attribute |
No | Page |
Example
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:url Tag Example</title>
</head>
<body>
<h1><c:url> Example Demo</h1>
<a href="<c:url value="http://www.tutorialpro.org"/>">
This link is generated by the <c:url> tag.
</a>
</body>
</html>
When accessing the above page through a browser and viewing the source code, it displays as follows: