BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSTL <c:set> Core Tag

Last Updated: November 28, 2013 by Chaitanya Singh | Filed Under: JSTL

<c:set> core JSTL tag is used for assigning a value to an object or variable within a specified scope. Let’s understand this with an example.

Here I’m assigning a string value to a variable name within application scope (it will let me access my variable in any of the JSP page across application). On the other page (display.jsp) I have printed the value on browser using <c:out> tag and EL.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Example of c:set tag</title>
</head>
<body>
<c:set var="name" scope="application" value="Chaitanya Pratap Singh"/>
<a href="display.jsp">Display</a>
</body>
</html>

display.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${name}"/>

Output:
Below are the screenshots of the above example’s output.

c-set-example

c-set-example2

Attributes of <c:set> tag

1) value: It can be a hardcoded value or an expression. for e.g. below are the allowed variations of <c:set> tag –

The value of the variable myvar would be stored in the object name.

<c:set var="name" scope="application" value="${myvar}"/>

The result of the expression would be stored in the object.

<c:set var="sum" scope="application" value="${1+3+6}"/>

2) var: It holds the variable/object name

3) scope: It can be request, session, page and application. In the above example we have specified the scope as application, however it can be anything out of the mentioned four. It all depends on the requirements.

Top Related Articles:

  1. JSTL <c:redirect> Core Tag
  2. JSTL <c:out> Core Tag
  3. fn:join() and fn:split() JSTL Functions
  4. JSTL <c:url> Core Tag
  5. JSTL <c:remove> Core Tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Binh Thanh Nguyen says

    September 25, 2015 at 9:00 AM

    Thanks, nice example.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap