Core JSTL c:set and c:remove example

In this tutorial, we will come to know how to use <c:set/> and <c:remove/> taglibs to set a value to an attribute in any scope and remove an attribute from any scope.

First of all, let us see the specifications of these two tags:

<c:set/>

This tag is used to set an attributes with some value in specified scope.Let us check out the different attributes that this can have :

cset attributes<c:set/> taglib can be use in two perspective :

  1. To set primitive values in an attribute of any scope like setting the value “Rahul” in attribute name “owner” in scope session.
  2. The second perspective may be like there is already an instance of Person class in attribute name “owner” in scope session and we want to set the property firstName of Person class.

In this example, we will learn the first perspective and then in next example we will go for second.

<c:remove/>

This tag is used to remove an attribute from an specified scope. Following are its attributes:

cremove attributesThe bellow example will explain the way to use the <c:set/> and <c:remove/> tag libs:

Tools used :

  • jstl 1.2

The example contains one jsp page in which we first set a scope variable using <c:set/> taglib and then we print the value of this scope variable to show the contained value and the we use <c:remove/> taglib to remove that scope variable. The code of this jsp file is as follows:


<%@page import="com.raistudies.domain.Person"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Example of c:set and c:remove taglibs</title>
    </head>
    <body>
        Setting value using c:out <c:set var="name" scope="request" value="Rai Studies" /><br>
        Value is: <b><c:out value="${name}"/></b><br>
        Removing value using c:remove <c:remove var="name" scope="request"/><br>
        Now, Value is: <c:out value="${name}"/><br>

    </body>
</html>

The output of the example is as follow:

cset and cremove example

cset and cremove example

You can see after removing the scope variable “name” using <c:remove/>, its value is not printed in last line of output.

You can try the example yourself by downloading the code of this example from bellow link:

Source + lib : Download

Related Posts:

Leave a Comment Cancel reply


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>