Set scoped variables with JSTL in JSP Page

In this example we shall show you how to set scoped variables in a JSP Page, using JSTL. The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. The JSTL tags can be classified, according to their functions, into Core tags, Formatting tags, SQL tags and XML tags and they can be used when creating a JSP page. To set variables with scope in a JSP Page, using JSTL one should perform the following steps:

  • Create a jsp page that contains the <%code fragment%> scriptlet. It can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
  • Include JSTL Core library in your JSP page, using the <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> syntax.
  • Use the <c:set> tag to set the result of an expression evaluation in a ‘scope’. It has three attributes, var that is the name of the variable to store information, value that is the information to save and scope that is the scope of variable to store information.
  • Use the <c:out> tag to display the result of an expression. In its value attribute you can set the information to output.
  • Add JSP comment using the <%-- --%> tags.

as described in the code snippet below.


<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/tld/c-rt.tld" prefix="c-rt" %>


	<title>Java Code Geeks Snippets - Set Scoped Variables with JSTL in JSP Page</title>

	<%-- Set scoped variables --%>
	<c-rt:set var="var1" value="value1" scope="page" />
	<c-rt:set var="var2" value="value2" scope="request" />
	<c-rt:set var="var3" value="value3" scope="session" />
	<c-rt:set var="var4" value="value4" scope="application" />
	<%-- Print the values --%>
	var1: <c-rt:out value='${pageScope.var1}' /> <br/>
	var2: <c-rt:out value='${requestScope.var2}' /> <br/>
	var3: <c-rt:out value='${sessionScope.var3}' /> <br/>
	var4: <c-rt:out value='${applicationScope.var4}' /> <br/>
	<c-rt:set var="color" value="#dddddd" />
	color: <c-rt:out value='${color}' /> <br/>




var1: value1
var2: value2
var3: value3
var4: value4
color: #dddddd

This was an example of how to set scoped variables in a JSP Page, using JSTL in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

I would like declare a variable in global state.It should be modified conditionally (when using c:if).How do we write it ? Thanks in Advance.

Back to top button