Back to: JSP Tutorials for Beginners and Professionals
JSP Scopes: Page, Request, Session, and Application
In this article, I am going to discuss JSP Scopes (Page, Request, Session, and Application). Please read our previous article where discussed JSP Comments. At the end of this article, you will understand the following pointers in detail.
- Scope of JSP Objects
- Type of Scopes in JSP
- JSP Page Scope with Example
- Advantages and Disadvantages of JSP Page Scope
- JSP Request Scope with Example
- Advantages and Disadvantages of JSP Request Scope
- JSP Session Scope with Example
- Advantages and Disadvantages of JSP Session Scope
- JSP Application Scope with Example
- Advantages and Disadvantages of JSP Application Scope
JSP Scopes
In J2SE applications, to define data availability i.e. scope for we have to use access specifiers public, protected, default, and private. Similarly, to form available data to a number of resources JSP technology has provided the scopes with the access modifiers. Objects, whether explicit or implicit in a JSP page, are accessible within a particular scope. For an explicit object, such as a JavaBean instance created in a jsp:useBean action, we can explicitly set the scope with the following syntax: scope=”scopevalue”
There are four possible scopes:
- Page Scope
- Request Scope
- Session Scope
- Application Scope
Page Scope in JSP
Page scope is managed by the pageContext object. As a page context object is created for every JSP page, so, every JSP page will have a specific page scope. PageScope is the default scope. The object is accessible only from within the JSP page where it had been created. A page-scope object is stored within the implicit pageContext object. The page scope ends when the page stops executing. If we declare the info in page scope by using pageContext object then that data should have the scope up to this JSP page. Note that when the user refreshes the page while executing a JSP page, new instances will be created of all page-scope object.
Page Scope Example in JSP
In this example, we have created two JSPs: “pagescope.jsp” and “pagescope2.jsp”. On pagescope.jsp we have created a local variable “name” which has a value “Manisha” and assigned “Page” scope. We also provided a link that points to another JSP page, i.e. “pagescope2.jsp”. So, when you will click on the link it did not print the variable value defined in it. Because the variable has “Page Scope” which is accessible on only that page and not on any other page.
pagescope.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> <c:set var="name" value="Manisha" scope="page" /> Local Variable : <c:out value="${name}" /> <a href="pagescope2.jsp">Test Page</a> </body> </html>
pagescope2.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
Run your code to get the following output:
After clicking on the “Test Page” link, you will get the following output:
Advantages of Page Scope:
- The page scope does not require explicit removal of created and bound objects.
Disadvantages of Page Scope:
- It is necessary to use page scope for objects created only for the current page, therefore it is rarely used.
Request Scope in JSP
Request scope is managed by the request object. This object is used for reading the request object. If the same request is shared by more than one JSP page those sharing the same request object come under the same request scope. Request Scope begins whenever the request object is created by the servlet container and the request scope ends whenever the request object is deleted by the servlet container. As the request object is stored in the pageContext object we can manage request scope attributes by using the pageContext object. The object is accessible from any JSP page servicing an equivalent HTTP request that’s serviced by the JSP page that created the thing AAA request-scope object is stored in the implicit request object. The request scope ends at the conclusion of the HTTP request. If we declare the info in the request object then that data should have the scope up to the number of resources that are visited by this request object.
Request Scope Example in JSP
In this example, we have created two JSPs: “requestscope.jsp” and “requestscope2.jsp”. On requestscope.jsp we have created a local variable name that has a value “Manisha” and assigned a “request” scope to it. We have also used jsp:forward tag to forward the same request to another JSP, i.e. requestscope2.jsp. Whenever we will run requestscope.jsp it does not print anything and forwards the request to requestscope2.jsp. Because the variable has a request scope which is accessible in the current request and on any page as long as the request remains the same.
requestscope.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> <c:set var="name" value="Manisha" scope="request" /> <jsp:forward page="requestscope2.jsp"></jsp:forward> </body> </html>
requestscope2.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
Run your code to get the following output:
Advantages of Request Scope:
- The objects are automatically released from memory when request processing completes.
- It is advantageous because explicit cleanup is not required and there is less risk of burdening the system with needless memory consumption.
Disadvantages of Request Scope:
- In the request scope, the objects created and bound to request scope are available to pages in that same request.
Session Scope in JSP
Session scope is managed by the session object. The same session object is available to all JSP pages as long as the session is not expired. Whenever a session is expired or session.invalidate() is called then a new session is created for the next requesting page. This object is used for reading attributes from HTTPSession. The object is accessible from any JSP page that’s sharing an equivalent HTTP session because of the JSP page that created the thing. A session-scope object is stored within the implicit session object. The session scope ends when the HTTP session times out or is invalidated. If we declare the info in HTTPSession object then that data should have the scope up to the number of resources that are visited by this client.
Example of Session Scope in JSP
In this example we have created two JSPs: sessionscope.jsp and sessionscope2.jsp. On sessionscope.jsp we have created local variable “name” which has a value “Manisha” and have assigned “session” scope to it. We also provided a link on sessionscope.jsp that points to another JSP, i.e. sessionscope2.jsp. So, when you will run the sessionscope.jsp page it will print the variable name. And when you will click on the link it goes to the second page and also printed the variable value defined in the first JSP. Because it has a session scope that is accessible in only that session so during the current session that variable can be accessed from any JSPs.
sessionscope.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> <c:set var="name" value="Manisha" scope="session" /> Local Variable : <c:out value="${name}" /> <a href="sessionscope2.jsp">Test Page</a> </body> </html>
sessionscope2.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
Run your code to get the following output:
After clicking on the “Test Page” link, you will get the following output:
Advantages of Session Scope:
- It is most commonly used because it allows you to create and bind objects to a session.
- It is often used for managing security credentials and for managing state among multiple pages.
Disadvantages of Session Scope:
- Objects bound to session scope should be explicitly removed when no longer needed to free up memory space.
Application Scope in JSP
The application scope is maintained by the application pointing object. Application scope begins whenever the ServletContext implementation object is created. Application scope ends whenever the ServletContext implementation object is deleted. This object is used for reading attributes from ServletContext. The object is accessible from any JSP page that’s utilized in an equivalent Web application because the JSP page that created the thing, within any single Java virtual machine. The concept is analogous thereto of a Java static variable. An application-scope object is stored within the implicit application servlet context object. The application scope ends when the appliance itself terminates, or when the JSP container or servlet container shuts down. If we declare the info in the ServletContext object then that data should have the scope up to the number of resources that are available within the present web application.
Application Scope Example in JSP
In this example, we have created two JSps: applicationscope.jsp and applicationscope2.jsp. On applicationscope.jsp we have created a local variable “name which has a value “Manisha” and assigned “application” scope to it. We have also provided a link on applicationscope.jsp that points to another JSP, i.e. applicationscope2.jsp. Whenever you will any of the JSP pages or you will click on the provided link it will print the variable value. Because the variable has an application scope and it remains accessible on any JSP page during the full application as long as it is running on the server.
applicationscope.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> <c:set var="name" value="Manisha" scope="application" /> Local Variable : <c:out value="${name}" /> <a href="applicationscope2.jsp">Test Page</a> </body> </html>
applicationscope2.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
Run your code to get the following output:
After clicking on the “Test Page”, you will get the following output:
Advantages of Application Scope:
- It is useful for storing information when we create objects bound at the application level in JSPs that are not session-aware.
- It is also useful to share data among different sessions of the same application.
Disadvantages of Application Scope:
- You should explicitly remove them to free up memory resources when you no longer need objects bound to an application.
Preferences
Below is given the following progression, from narrowest scope to broadest scope:
page < request < session < application
If you would like to share an object between different pages in an application, like when forwarding execution from one page to a different, or including content from one page in another, you can’t use page scope for the shared object. In this case, there would be a separate object instance related to each page. The narrowest scope you’ll use to share an object between pages is a request
In the next article, I am going to discuss JSP Implicit Objects with Examples. Here, in this article, I try to explain JSP Scopes (Page, Request, Session, and Application). I hope you enjoy this JSP Scopes article.