What are the different scopes in JSP?

By: Barbara Emailed: 1769 times Printed: 2517 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

One of the most powerful features of JSP is that a JSP page can access, create, and modify data objects on the server. You can then make these objects visible to JSP pages. When an object is created, it defines or defaults to a given scope. The container creates some of these objects, and the JSP designer creates others.

The scope of an object describes how widely it's available and who has access to it. For example, if an object is defined to have page scope, then it's available only for the duration of the current request on that page before being destroyed by the container. In this case, only the current page has access to this data, and no one else can read it. At the other end of the scale, if an object has application scope, then any page may use the data because it lasts for the duration of the application, which means until the container is switched off.

Page Scope

Objects with page scope are accessible only within the page in which they're created. The data is valid only during the processing of the current response; once the response is sent back to the browser, the data is no longer valid. If the request is forwarded to another page or the browser makes another request as a result of a redirect, the data is also lost.

//Example of JSP Page Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="page" />

Request Scope

Objects with request scope are accessible from pages processing the same request in which they were created. Once the container has processed the request, the data is released. Even if the request is forwarded to another page, the data is still available though not if a redirect is required.

//Example of JSP Request Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="request" />

Session Scope

Objects with session scope are accessible from pages processing requests that are in the same session as the one in which they were created. A session is the time users spend using the application, which ends when they close their browser, when they go to another Web site, or when the application designer wants (after a logout, for instance). So, for example, when users log in, their username could be stored in the session and displayed on every page they access. This data lasts until they leave the Web site or log out.

//Example of JSP Session Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="session" />

Application Scope

Objects with application scope are accessible from JSP pages that reside in the same application. This creates a global object that's available to all pages.

Application scope uses a single namespace, which means all your pages should be careful not to duplicate the names of application scope objects or change the values when they're likely to be read by another page (this is called thread safety). Application scope variables are typically created and populated when an application starts and then used as read-only for the rest of the application.

//Example of JSP Application Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="application" />

JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Precise information in the article, however it would be very nice if you could have just mentioned a line or two showing how to declare variables of each scope.


View Tutorial          By: Ketan at 2009-06-23 05:07:39
2. View Comment

very useful article. every jsp resource will get good knowledge from this.

View Tutorial          By: pechirani at 2011-06-29 04:00:49
3. View Comment

Excellent information in simple language. Its worthy to bookmark this site.

View Tutorial          By: Rishi at 2011-11-04 05:03:24
4. View Comment

Excellent information reg the scopes of jsp , please elaborate more with simple example or how to use these scopes.

View Tutorial          By: venkateshwarreddy suravaram at 2012-01-09 08:34:23
5. View Comment

This is very much useful to me to complete my assignment..


View Tutorial          By: Eva Jhonson at 2012-09-30 16:44:08
6. View Comment

what is differences between application scope and session scope in a session time?

View Tutorial          By: suresh at 2013-05-13 13:03:12
7. View Comment

Great article! Simple and precise. Thanks!

View Tutorial          By: dz at 2014-10-06 04:36:56
8. View Comment

Excellent information

View Tutorial          By: mahesh at 2014-10-18 06:07:35
9. View Comment

Very precisely and simply explained.

View Tutorial          By: Deepak at 2015-01-05 08:37:01
10. View Comment

Great Job

View Tutorial          By: Niteen Dhule at 2015-02-09 06:37:03
11. View Comment

thanks! it helped!

View Tutorial          By: shgy at 2015-03-17 07:42:40
12. View Comment

Very useful n understandable. More understandable if u give program example .

View Tutorial          By: Kiran at 2015-08-27 06:13:00

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Barbara
Configuring JDBC DataSources in Struts
What are the different scopes in JSP?
Drawbacks of the JSP Model 1 Architecture
Struts Classes

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
What are the different scopes in JSP?
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
Automatically Refreshing a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Embedding java codes in jsp sciptlets
JSP Program for display Date
The Servlet Life Cycle
Enabling Expression Language Evaluation in JSP
Deploying an Individual JSP on Tomcat
The page Directive in JSP
Declarations in JSP
The Request Object in JSP
JSP CheckBox Example
What is JSP?
Most Emailed Articles (in JSP)
What is JSP?
Declaring variable in JSP
Techniques for form editing
A Simple Servlet Generating Plain Text
An Example Using Servlet Initialization and Page Modification Dates
Text Object
The Basic Syntax Expression Language in JSP
Disabling Scriptlets in JSP using web.xml
The BodyTag Interface in JSP
What are the different scopes in JSP?
Sending Email using JSP
Using a DataSource from WebLogic in a JSP
Getting HTTP Request Headers in a JSP