Java/JSTL/Variable Scope
JSTL Set Variables Scope
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>
<head> <title>Set in Scope Examples</title> </head> <body> <c:set var="test" value="Page Level Value" scope="page" /> <c:set var="test" value="Request Level Value" scope="request" /> <c:set var="test" value="Session Level Value" scope="session" /> <c:set var="test" value="Application Level Value" scope="application" />
Default Level |
<c:out value="${test}" /> |
Page Level |
<c:out value="${pageScope.test}" /> |
Request Level |
<c:out value="${requestScope.test}" /> |
Session Level |
<c:out value="${sessionScope.test}" /> |
Application Level |
<c:out value="${applicationScope.test}" /> |
</body>
</html>
</source>
Variable scope: page, session and application
<source lang="java">
//File: index.jsp <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <c:set var="scopeVarPage" value="Page Value" scope="page" /> <c:set var="scopeVarRequest" value="Request Value" scope="request" /> <c:set var="scopeVarSession" value="Session Value" scope="session" /> <c:set var="scopeVarApplication" value="Application Value" scope="application" /> <html>
<head> <title>Scope Example</title> </head> <body>
Main File: index.jsp
Scoped Variable | Current Value |
---|---|
Page Scope(scopeVarPage) |
<c:out value="${scopeVarPage}" /> |
Request Scope(scopeVarRequest) |
<c:out value="${scopeVarRequest}" /> |
Session Scope(scopeVarSession) |
<c:out value="${scopeVarSession}" /> |
Application Scope(applicationVarPage) |
<c:out value="${scopeVarApplication}" /> |
<jsp:include page="included.jsp" />