Scope defines the reach and lifespan of objects created by a web application.
There are three main scopes in servlet technology:
-
The REQUEST scope is limited to the lifespan of a single HTTP request, and it is private to that specific HTTP request.
This means that other HTTP requests cannot access this scope or its objects.
Objects stored in REQUEST scope are automatically destroyed when the HTTP response is sent back to the client.
-
The SESSION scope is limited to the lifespan of the HTTP session, and it is private to that specific session.
This means that only HTTP requests from the same session can access this scope and its objects.
Objects attached to the SESSION scope persist across multiple HTTP requests within the same session.
The session typically expires after a period of inactivity or when explicitly invalidated.
If multiple HTTP requests are executed at the same time within the same session, concurrent access to SESSION scope objects is possible.
-
The APPLICATION scope is limited to the lifespan of the web application, and it is shared across all users and sessions within that application.
This means that all HTTP requests from the same web application can access this scope and its objects.
Objects attached to the APPLICATION scope persist across multiple requests, sessions, and users until the web application is shut down or redeployed.
Concurrent access to APPLICATION scope objects is possible and requires proper synchronization for thread safety.