Variable scopes

We can control the scope of a variable created with <c:set> by using the scope attribute. The possible values for the scope attribute are

  • page -> accessible only within the current jsp page.
  • request -> accessible by a all jsp pages in the same request.
  • session -> accessible for the entire session.
  • application -> accessible throughout the application.

Edxample of JSTL Core Variable Scopes

HTML




<%@ 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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:set var="count" value="42" scope="session" />
    <p>Count in session: ${count}</p>
</body>
</html>


Output:

JSTL Core Tag

The <c:set> tag is used for variable creation and assignment in JSP pages. In this article, we will delve into the JSTL Core <c:set> tag, provide code samples, and illustrate the expected outputs. It is used to set the result of an expression evaluated in a “scope”.

Prerequisite of the topic

  • Good understanding of Java EE web project developments like setting up a servlet or a JSP page with the help of any IDE
  • Any IDE with support for Java EE development.
  • An installation of Apache Tomcat web server or any other server that supports running Java-based web applications like GlassFish, Websphere, etc.

Syntax

<c:set  var="string"  value="string" target="string" property="string>  scope="string"/>

Similar Reads

Attributes of

...

Declaring a variable

To declare any variables use the c:set tag to define the variable, and use the var attribute to name the object to be referenced later. Use the value attribute to set the actual value to the object. Once the variable is declared use expression syntax to access the object in that page. We can also use the tag to display the expression....

Example of JSTL Core Tag

Below is the implementation of the above topic:...

Variable scopes

...

Conclusion

We can control the scope of a variable created with by using the scope attribute. The possible values for the scope attribute are...

Contact Us