Set scoped variables with JSTL in JSP Page (original) (raw)

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:

as described in the code snippet below.

SetScopedVariablesJSTL.jsp

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

Java Code Geeks Snippets - Set Scoped Variables with JSTL in JSP Page
<%-- 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/>

URL:

http://localhost:8080/jcgsnippets/SetScopedVariablesJSTL.jsp

Output:

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.

Photo of 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.

Back to top button