Open In App

JSTL Core <c:url> Tag

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To construct and manage URLs within JSP pages we have this tag <c:url> for your JSP application. It is among the most fundamental tags within JSTL’s core library. Purpose of <c:url> tag.

Uses of JSTL Core <c:url>

The <c:url> tag primarily serves two key purposes:

  1. URL Formatting: The <c:url> tag effectively formats URLs, ensuring they adhere to the proper syntax and structure. This includes handling special characters and ensuring proper encoding.
  2. URL Rewriting: When necessary, the <c:url> tag automatically performs URL rewriting. This involves incorporating session IDs and context paths to ensure URLs are valid and accessible.

Structure and Syntax of the <c:url> Tag

The <c:url> tag follows a well-defined structure, consisting of several attributes that control its behavior:

<c:url value="/path/to/resource" var="urlVariable" scope="request|page|session"/>
  • value: Specifies the URL to be formatted or rewritten.
  • var: Defines the name of the variable that will hold the formatted URL.
  • scope: Determines the scope of the variable, indicating where it will be accessible within the JSP page. Options include request, page, or session.

Example Usage of the <c:url> Tag

Demonstration of <c:url> tag in An online store application needs to display product details for each item listed on the homepage. Each product has an associated image stored in the application’s image directory.

Below is the implementation of JSTL core <c:url>:

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
<head>
<title>Product Listing</title>
</head>
<body>
  
<h1>Product Listing</h1>
  
<c:forEach var="product" items="${productList}">
  <div class="product-item">
    <h2>${product.name}</h2>
      <p>Price: ${product.price}</p>
          <c:url value="/images/${product.image}" var="imageSrc" scope="page"/>
          <img src="${imageSrc}" alt="${product.name}" />
        <a href="<c:url value="/product/${product.id}"/>">View Details</a>
  </div>
</c:forEach>
  
</body>
</html>


Output:

product.name: DSA-Course 
product.price: 5000
product.image: DSA-image.jpg
product.id: 101

Explanation of the above Program:

The <c:forEach> tag iterates over a list of products stored in the productList variable.

  • Inside the <c:forEach> loop, each product’s name, price, and image are displayed.
  • The <img> tag utilizes the <c:url> tag to construct the URL for the product’s image. The contextPath variable refers to the application’s context path, which is typically set in the web.xml file.
  • The <a> tag creates a link to the product detail page using the <c:url> tag. The product’s ID is passed as a parameter to the URL.

This example demonstrates how the <c:url> tag can be used to dynamically generate URLs based on data retrieved from the application’s data model, ensuring that the URLs are always valid and accessible.

Benefits of Using the <c:url> Tag

The <c:url> tag offers several advantages for JSP developers:

  • Simplified URL Construction
  • Reduced Errors
  • Improved Readability
  • Consistent URL Handling

Conclusion

JSTL Core <c:url> tag is a proven tool in the field of JSP application development. It helps to simplify and implement various features such as

  • URL construction
  • Ensures proper encoding
  • URL rewriting
  • Improves quality
  • Easy maintainability
  • Robust Mechanism

Overall it allows the developers to work with URLs in a consistent and reliable manner.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads