Working with variables by using c:set and c:remove actions

JSTL provides c:set action to initialize or set a variable of the web application in a specific scope. The usage of the c:set is as follows:

<c:set var = "variable"
       value = "value"
       scope = "scope" />

In the var attribute, you can declare or refer to a variable. The value attribute specifies the value of the variable. If you want to set the scope of the variable, you can use the scope attribute. The scope attribute accepts any valid JSP variable scopes such as page, request, session, and application.

Let’s take a look at an example of using  c:set action.

<c:set var = "userid"
       value = "10"
       scope = "session" />

<c:out value="${userid}" />Code language: HTML, XML (xml)

In the example, we set the variable userid to 10 with the session scope. Then we print it out using c:out action.

The other usage of  c:set action is to set the value of a property of an object:

<c:set target= "object"
       property = "property name"
       value = "value of property" />Code language: HTML, XML (xml)

You assign an object to the target attribute, property name to the property attribute and value of that property in the value attribute.

Let’s take a look at an example of using c:set for setting a property of an object.

package com.jsptutorial;

public class Person {

    private String firstName;
    private String lastName;

    public Person() {
        this.firstName = "";
        this.lastName = "";
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}Code language: JavaScript (javascript)

In the above code, we have a person class. We will use this person class as a JavaBean in a JSP page.

<%@page contentType="text/html" 
        pageEncoding="UTF-8"
        import="com.jsptutorial.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
          prefix="c" %>
<html>
    <head>
        <title>JSTL demo</title>
    </head>
    <body>
        <jsp:useBean id="person"
                     class="com.jsptutorial.Person" />

        <c:set target="${person}"
               property="firstName"
               value="Jack" />
        <c:set target="${person}"
               property="lastName"
               value="Daniel" />

        <c:out value="${person.fullName}" />
    </body>
</html>Code language: HTML, XML (xml)

In the code above, we use the standard action useBean to initialize an object person. Then we use  c:set to set the firstName and lastName property of that object. We display the fullName of  the person object to the web browser.

Notice that that in the target attribute, only object is accepted therefore you should not forget the expression  ${object_name}

Beside c:set action, JSTL also provides c:remove action that allows you to remove a variable from a particular scope. The syntax of c:remove is action is as follows:

<c:remove var = "variable name"
          scope = "scope" />Code language: HTML, XML (xml)

For example, if you want to remove the userid session variable you can use c:remove as follows:

<c:remove var="userid" scope="variable" />Code language: HTML, XML (xml)