c:set allows setting 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 variable's scope to save the information. Valid values are page, request, session, and application. The following is a detail of the values available in the scope:
  1. page: variables set with c:set in this scope are accessible only within the current JSP page.
  2. request: variables set with c:set in this scope are accessible throughout the current request. This means the variables are accessible in all JSP pages involved in the same request, including any forwarded or included pages.
  3. session: variables set with c:set in this scope are accessible for the entire session.
  4. application: variables set with c:set in this scope are accessible for all application users.

<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.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram