JSTL c:set Example

JSTL c:set Example explains about How to use c:set tag inside JSTL for defining a variable

Consider an example, where you need to define a variable and store some values into that variable, In that case you can use JSTL c:set Example

c:set is available as a part of JSTL core tags, this tag evaluates an expression and can be set to a variable, so that we can reuse that variable in other places

Inside c:set tag, we can also customize the scope of the variable whether it is for page,request,session or application.

You can see the below demonstrating the usage JSTL c:set Example inside JSTL

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL c:set Example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<title>JSTL c:set Example</title>
	</head>
	<body>
		<!-- JSTL c:set Example -->
		<p>
			<c:set scope="request" value="Rockey" var="name" />
			<c:set scope="session" value="25" var="age" />
			<c:set scope="application" value="C/3343" var="address" />
			<c:set value="Delhi" var="city" />
			<c:out value="${age}" />
			<c:out value="${name}" />
			<c:out value="${address}" />
			<c:out value="${city}" />
		</p>
	</body>
</html>
Output
25 Rockey C/3343 Delhi

you can also use c:set on the following ways

 

 

 

 

 

 

 

 

 

 

 

 

 

<c:set property="name" target="nameMap" value="Jose"></c:set> Here target is a real object we are putting a value as nameMap.put("name", "Jose") <c:set property="name" target="person" value="Jose"></c:set> Here target is a real object we are setting a property as person.setName("name", "Jose")

 

 

 

 

 

 

 

 

 

 

 

 

 











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