beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

JSTL <c:set> Core Tag

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.

Enjoyed this post? Try these related posts

  1. JSTL <c:import> Core Tag
  2. fn:toLowerCase() – JSTL Function
  3. JSTL <c:out> Core Tag
  4. fn:contains() – JSTL Function
  5. fn:escapeXml() – JSTL Function
  6. fn:endsWith() – JSTL Function

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()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap