JSTL Core Tag c:url Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
In this post, you will learn how to use the <c:url> tag in the JSTL core tags library.
You know, the <c:url> tag creates a URL and optionally stores the URL in a variable. We can use <c:param> to supply request parameters and values optionally.
JSTL <c:url> Syntax:
<c:url
var="<string>"
scope="<string>"
value="<string>"
context="<string>"/>
Attributes:
Name | Required | Type | Description |
var | False | java.lang.String | The name of the variable to store the resulted URL. |
scope | False | java.lang.String | The scope in which the resulted URL to be placed. |
value | False | java.lang.String | URL string to be used. |
context | False | java.lang.String | Name of the web application context when referencing external resources with relative path. |
JSTL <c:url> Example:
The below example creates a URL to a resource.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title><c:url> Demo</title> </head> <body> <h1><c:url> Demo</h1> <a href="/<c:url value="http://www.codejava.net"/>"> This is a URL formed by <c:url> tag. </a> </body> </html>
We can supply request parameters optionally to the URL as shown below.
<c:url value="/getItemDetails.jsp" var="itemDetailsURL"> <c:param name="itemId" value="I1980X"/> </c:url> <a href="/<c:out value="${itemDetailsURL}"/>">Click to get item details</a>
Output:
Recommended Usage of JSTL <c:url> tag:
Useful to create URL string and needs to store the URL in any scope for later use. Optionally we can send parameters using the tag <c:param>.
Other JSTL Core Tags:
if | catch | choose | forEach | forTokens | import | out | param | redirect | remove | set
Comments
I have a series of Spring tutorials here: www.codejava.net/spring-tutorials