JSP - JSTL Core <c:remove> Tag

<C:remove> tag removes a particular variable from a given scope, and if a scope is not already mentioned, then it looks for it in the (page, request, session, and application) and removes it if found there.

<c:remove> Tag Syntax

Syntax:
<c:remove var="variable_name" scope="scope_name"/>

<c:remove> Tag Attributes

AttributeRequiredDescription
varYesThe name of the variable to be removed.
scopeNoThis attribute defines the scope of the variable from where it is to be removed. Valid values are page, request, session, and application.

<c:remove> Tag Example

Here is an example where this <c:remove> tag removes a variable from the session's scope, but first, this variable must exist within the session. So here we are going to set a session variable using the <c:set> tag.

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

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

<body>
    <c:set var="userid" scope="session" value="${199}" />
</body>

</html>
Output:
The defined value in the session scope is: 199

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.

Now, if the application needs to remove this session variable when the user logs out, then the <c: remove> tag can be used here as per the example below.

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

<!DOCTYPE html>
<html>
<head>
    <title>Example of remove a session in core-JSTL</title>
</head>

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

</html>
Output:
The defined value in the session scope is: 199
The value after removal is:

Scroll Back to Top