JSTL Core <c:set> Tag

Introduction :

The <c:set> tag allows to set the value of a variable or property into the given scope. Using this tag we can set the property of bean and also we can set the values of map.

JSTL <c:set> also called as JSTL Core tag library is a good replacement of <jsp:setProperty> jsp action which lacks lot of functionality and only allow you to set bean property. you can not set Map's key value or create a scoped variable by using <jsp:setProperty>. jstl <set> tag allows you to do all the stuff related to setting or creating variables or attributes.

By using JSTL <c:set>tag you can do following operations.

  • Set bean properties
  • Set Map values
  • Create Scoped variable on page, request, session or application scope.

Introduction :

The <c:set> tag allows to set the value of a variable or property into the given scope. Using this tag we can set the property of bean and also we can set the values of map.

JSTL <c:set> also called as JSTL Core tag library is a good replacement of <jsp:setProperty> jsp action which lacks lot of functionality and only allow you to set bean property. you can not set Map's key value or create a scoped variable by using <jsp:setProperty>. jstl <set> tag allows you to do all the stuff related to setting or creating variables or attributes.

By using JSTL <c:set>tag you can do following operations.

  • Set bean properties
  • Set Map values
  • Create Scoped variable on page, request, session or application scope.



Syntax of c:set tag

<c:set attribute> body content </c:set>

Attributes of <c:set> tag:

  1. value: The value attribute is the expression to be evaluated.
  2. var: This attribute is used to display the name of the variable to store the information.
  3. scope: This attribute specifies the scope of variable to store information.Valid values are page,request,session and application.
  4. target: This attribute is used to display the name of the variable to store the information.
  5. property: This attribute is the name of the property to be set of the object specified by the target attribute.

Example of c:set tag :

Below example will describe the real use of c:set tag:

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<html>
<head>
<title>C:Set Tag Example</title>
</head>
<body>
<h2>This is C:Set Tag Example</h2>
<c:set var="firstName" value="Mukesh" />
<c:set var="lastName" value="Kumar" />
<h3>
Welcome
<c:out value="${firstName}" />
<c:out value="${lastName}" />
</h3>
</body>
</html>

As you can see in above code,we first set a scope variable using <c:set> tag and later we print the value of this scope variable using <c:out>.


Output in Browser :

JSTL c:set tag


Download this example(src+lib) developed in eclipse





comments powered by Disqus