JSP Object Scope
Defining Scope for a JSP Object?
Explanation
The availability of an JSP object to be used is usually defined by the scope of that object.
Page Scope:
Using this the JSP object can be used within the page whee it was created.
Request Scope:
Using this the JSP object can used from anywhere the request is being served.
Session scope:
Using this the JSP object can be used from pages belonging to the same session.
Application scope:
Using this the JSP object can used from pages across the application.
Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="Pag" value="Page Value" scope="page" />
<c:set var="Req" value="Request Value" scope="request" />
<c:set var="Ses" value="Session Value" scope="session" />
<c:set var="App" value="Application Value" scope="application" />
<html>
<body>
<b>Page Scope</b> ::<c:out value="${Pag}" /><br>
<b>Request Scope</b> ::<c:out value="${Req}" /><br>
<b>Session Scope</b> ::<c:out value="${Ses}" /><br>
<b>Application Scope</b>::<c:out value="${App}" /><br>
<a href="scope2.jsp">Go nextpage for Session,Application Scope</a>
</body>
</html>
Result
Page Scope ::Page Value
Request Scope ::Request Value
Session Scope ::Session Value
Application Scope::Application Value
Goto to next page to know the Session,Application Scope
Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<b>Page Scope</b> ::<c:out value="${Pag}" /><br>
<b>Request Scope</b> ::<c:out value="${Req}" /><br>
<b>Session Scope</b> ::<c:out value="${Ses}" /><br>
<b>Application Scope</b>::<c:out value="${App}" /><br>
</body>
</html>
Result
Page Scope ::
Request Scope ::
Session Scope ::Session Value
Application Scope ::Application Value
In the above example "scope1.jsp" variables are set in all the four scopes are displayed. When clicked on a link and moved to the next page "scope2.jsp" to display all the variables only the session and application scope variables are displayed as there is no page, request scope for the second page.