Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

Scopes

You can store the data in different container objects to access later, depending on the requirement.
These objects are called "scoped objects.
You can store the data in the required scope as an attribute.
An attribute is a name-value pair.
The name of an attribute is of type String.
The value of an attribute is of type "object.
Attributes are read-write, i.e., you have to store the attributes so that you can collect them later.
You can use the following methods to manage the attributes:

void setAttribute(String,Object)
Object getAttribute(String)
void removeAttribute(String)
Enumeration getAttributeNames()

There are 3 scopes in Servlet.

1. Request Scope
2. Session Scope
3. Context Scope

1. Request Scope

When the data is stored in the HttpServletRequest object, the scope of that data is the request scope.
The data from the request scope can be accessed by that single user in that request only before sending the response to the client.

2. Session Scope

When the data is stored in the HttpSession object, then the scope of that data is session scope.
The data from session scope can be accessed by a single user across multiple requests (within the same session).

3. Context Scope

When the data is stored in the ServletContext object, then the scope of that data is context scope.
The data from context scope can be accessed by multiple users across multiple requests.

There are 4 scopes in JSP.

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

1. Page Scope

The data from page scope can only be accessed within the same JSP.

2. Request Scope

When the data is stored in the HttpServletRequest object, the scope of that data is the request scope.
The data from the request scope can be accessed by that single user in that request only before sending the response to the client.

3. Session Scope

When the data is stored in the HttpSession object, then the scope of that data is session scope.
The data from session scope can be accessed by a single user across multiple requests (within the same session).

4. Application Scope/Context Scope

When the data is stored in the ServletContext object, then the scope of that data is context scope.
The data from context or application scope can be accessed by multiple users across multiple requests.