Open In App

JSTL Core <c:remove> Tag

Last Updated : 15 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The JSTL Core <c:remove> tag is used to remove a variable or attributes from a specific scope in your JSP application used for making web pages in Java. It can be used to clean up any scoped resources that a JSP is responsible for.

The <c:remove> tag in JSP development. It allows you to remove a variable or attributes from one of the four possible scopes:

  1. Page Scope
  2. Session Scope
  3. Request Scope
  4. Application Scope

Syntax for <c:remove> tag

The syntax for using the <c:remove> tag is relatively simple:

<c:remove var="variableName" scope="scopeName" />

Attributes:

  • var (required): The name of the variable to be removed.
  • scope (optional): Information about the place where the variable is present. If no information(about scope) is provided, the variable will be removed from all scopes.

Example:

In this example, a variable named income is set in the session scope and then the following steps occurred.

  • firstly, income is displayed
  • then it is removed
  • then displayed again.

The output shows how the value changes before and after removal.

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
  <head>
      <title>JSTL Core <c:remove> Tag</title>
  </head>
    <body>
  
      <c:set var="income" scope="session" value="${4000*4}" />
  
      <p>Before Remove Value is: <c:out value="${income}" /></p>
  
      <c:remove var="income" scope="session" />
  
      <p>After Remove Value is: <c:out value="${income}" /></p>
  
    </body>
</html>


Output:

Before Remove Value is: 16000
After Remove Value is: null

Usage Scenarios:

The <c:remove> tag can be used for various purposes, such as:

  • When a user logs out this tag can be used to clean variables.
  • <c:remove> tag can be used to remove the temporary variables.
  • To reset the value of a variable before it is used again.

Note:

  1. Ensure you must add the following taglib, in order to import and use all the core tags in our code :
    <%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
  2. Save the code with .jsp extension then run on live server.

Benefits of <c:remove>

Using the <c:remove> tag has several benefits, such as:

  • By freeing up memory resources it improves the JSP application’s performace.
  • It makes JSP applications more maintainable by making it clear which variables are being removed from which scopes.
  • when the <c:remove> tag is used properly by cleaning the variables, it prevent memory leaks.

Conclusion:

The JSTL Core <c:remove> is an important and useful tag when it comes to the JSP application’s performance, as it helps in cleaning up and managing memory issues created by scoped variables.

Thus improving the overall performance of JSP applications and making the web pages work better and more efficient.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads