c:set allows to set the result of an expression in a variable within a given scope. Using this tag helps to set the property of 'JavaBean' and the values of the 'java.util.Map' object.

The JSTL <c:set> tag is similar to the <jsp:setProperty> JSP action tag, but this JSP action tag only allows setting the bean property and cannot set the value of a map key or create a scope variable.

You can perform the following operations using the JSTL <c:set> tag:

  • Set JavaBean property.
  • Set java.util.Map object value.
  • Create a scoped variable on a page, request, session, or application scope.

<c:set> Tag Syntax

Syntax:

<c:set attributes />

<c:set> Tag Attributes

Attribute Required Default Description
value No Body This attribute is to evaluate an expression and save information.
target No None This attribute is used to display the name of an object to save information.
property No None This attribute is the property name of the object to set the value specified by the target attribute.
var No None The name of the variable to save the information.
scope No Page This attribute defines the scope of the variable to save the information. Valid values are page, request, session, and application.

<c:set> Tag Example

Here is an example where this <c:set> Tag stores information on the session scope:

Example:

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<!DOCTYPE html>
<html>
<head>
    <title>Example of set in core-JSTL</title>
</head>

<body>
    <c:set var = "userid" scope = "session" value="${204}" />
    The defined value in the session scope is: <c:out value = "${userid}"/>
</body>

</html>

Output:

The defined value in the session scope is: 204

The example code above sets a session scope variable using the '<c:set>' tag and later prints the value of this scope variable to the next line using the '<c:out>' tag.