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 >>






No comments:

Post a Comment