JSP

JSP Scopes Example

JSP provides very useful features. One of them is maintaining the user defined variable. As you all know that in JAVA, each variable has a scope. Scope decides the accessibility of an object or variable. For example, some variables are accessible within for loop, if-else block or within specific method or class or package.

Same way, in JSP some variables needs to have different scopes than others. JSP provides the capability to user to define the scope of these variables.

Type of Scopes in JSP:

JSP provides 4 scopes to a variable. Developer can assign any one of them to a variable.

1. Page Scope.
2. Request Scope.
3. Session Scope.
4. Application Scope.

JSP Page Scope Example

Page Scope makes variable available to the developer for the current page only. Once the current page is closed by user or forwarded internally by application or redirected by application, than the variables having page scope will not be accessible on next page.

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Page Scope Example</title>
</head>
<body>
 <c:set var="name" value="Dinesh" scope="page" />
 Local Variable : <c:out value="${name}" />
 <a href="test.jsp">Test Page</a>
</body>
</html> 

Output :
Local Variable: Dinesh
Test Page

Second JSP File(test.jsp):

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <title>JSP Page Scope Example</title>
</head>
<body>
 Variable From previous page : <c:out value="${name}" />
</body>
</html>

Output: Variable from previous page:

As you can see above, we created 2 JSPs. On First JSP, we created local variable ‘name’ using JSTL Core tag c:set. This variable has a value ‘Dinesh’ and assigned ‘Page’ scope using scope attribute of c:set tag. We also provided a link on the same JSP that points to another JSP.

When we run the first JSP, it printed variable name successfully and also provided link on the browser.

On Second JSP, we again tried to print the same variable from the first page. So when user click on the link provided on first JSP, it goes to second JSP. However, when second JSP compiled and run, it did not print the variable value defined in first JSP.

This example result of 2 JSPs confirms that, if a variable has Page Scope, it is accessible in only that page and not on any other page.

JSP Request Scope Example

Request Scope makes variable available to the developer for the current request only. Once the current request is over, than the variables having request scope will not be accessible on next request. Single request may include multiple pages using forward.

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Request Scope Example</title>
</head>
<body>
 <c:set var="name" value="Dinesh" scope="request" />
 <jsp:forward page="test.jsp"></jsp:forward>
</body>
</html> 

Second JSP File(test.jsp):

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <title>JSP Request Scope Example</title>
</head>
<body>
 Variable From previous page : <c:out value="${name}" />
</body>
</html>

Output: Variable from previous page: Dinesh

As you can see above, we created 2 JSPs. On First JSP, we created local variable ‘name’ using JSTL Core tag c:set. This variable has a value ‘Dinesh’ and assigned ‘request’ scope using scope attribute of c:set tag. We also use jsp:forward tag to forward the same request to another JSP.

When we run the first JSP, it does not print anything on the browser and forwards the request to another JSP. However, it creates a variable before forwarding.

On Second JSP, it prints the variable value defined in first JSP.

This example result of 2 JSPs confirms that, if a variable has Request Scope, it is accessible in current request and on any page as long as request remains same.

JSP Session Scope Example

Session Scope makes variable available to the developer for the current session only. Once the current session is over or timed out, than the variables having session scope will not be accessible on next session.

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Session Scope Example</title>
</head>
<body>
 <c:set var="name" value="Dinesh" scope="session" />
 Local Variable : <c:out value="${name}" />
 <a href="test.jsp">Test Page</a>
</body>
</html> 

Output:
Local Variable: Dinesh
Test Page
Second JSP File(test.jsp):

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <title>JSP Session Scope Example</title>
</head>
<body>
 Variable From previous page : <c:out value="${name}" />
</body>
</html>

Output: Variable from previous page: Dinesh

As you can see above, we created 2 JSPs. On First JSP, we created local variable ‘name’ using JSTL Core tag c:set. This variable has a value ‘Dinesh’ and assigned ‘Session’ scope using scope attribute of c:set tag. We also provided a link on the same JSP that points to another JSP.

When we run the first JSP, it printed variable name successfully and also provided link on the browser.

On Second JSP, we again tried to print the same variable from the first page. So when user click on the link provided on first JSP, it goes to second JSP. When second JSP compiled and run, it also printed the variable value defined in first JSP.

This example result of 2 JSPs confirms that, if a variable has Session Scope, it is accessible in only that session. During the current session, variable can be accessed from any JSPs.

JSP Application Scope Example

Application Scope makes variable available to the developer for the full application. It remains available till application is running on server.

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Application Scope Example</title>
</head>
<body>
 <c:set var="name" value="Dinesh" scope="application" />
 Local Variable : <c:out value="${name}" />
 <a href="test.jsp">Test Page</a>
</body>
</html> 

Output:
Local Variable: Dinesh
Test Page

Second JSP File(test.jsp):

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <title>JSP Application Scope Example</title>
</head>
<body>
 Variable From previous page : <c:out value="${name}" />
</body>
</html>

Output: Variable from previous page: Dinesh

As you can see above, we created 2 JSPs. On First JSP, we created local variable ‘name’ using JSTL Core tag c:set. This variable has a value ‘Dinesh’ and assigned ‘Application’ scope using scope attribute of c:set tag. We also provided a link on the same JSP that points to another JSP.

When we run the first JSP, it printed variable name successfully and also provided link on the browser.

On Second JSP, we again tried to print the same variable from the first page. So when user click on the link provided on first JSP, it goes to second JSP. When second JSP compiled and run, it also printed the variable value defined in first JSP.

This example result of 2 JSPs confirms that, if a variable has Application Scope, it remains accessible in any JSP during the full application as long as it is running on server.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago