MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Java Servlet | Scopes
  1. Introduction to Servlet Scopes
  2. REQUEST Scope
  3. SESSION Scope
  4. APPLICATION Scope

  1. Introduction to Servlet Scopes
    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.
  2. REQUEST Scope
    This scope is ideal for storing data that needs to be shared between servlets during request forwarding or including, such as when using RequestDispatcher.

    The REQUEST scope is created for each HTTP request received by the servlet container and remains available until the HTTP response is generated and sent back to the client.

    The REQUEST scope becomes available when the target servlet for the HTTP request is executed (when the service method is called).

    If filters must be executed first, the REQUEST scope becomes available when the first filter is executed (when the doFilter method is called).

    The REQUEST scope is represented by an instance of the HttpServletRequest class.

    Objects can be stored in REQUEST scope using the setAttribute(String name, Object value) method and retrieved using the getAttribute(String name) method.

    REQUEST scope attributes are automatically cleaned up when the request processing is complete, making it memory-efficient for temporary data storage.
  3. SESSION Scope
    The session enables tracking of the user's HTTP requests across multiple interactions, maintaining state between requests.

    Session tracking is typically implemented using cookies (JSESSIONID), URL rewriting, or SSL session tracking. The servlet container automatically manages session creation, maintenance, and cleanup.

    The SESSION scope is created by the servlet container when the application decides to establish a session with the user, typically when getSession() is called for the first time.

    The SESSION scope is represented by an instance of the HttpSession class.

    This instance is available by calling the getSession() method from the REQUEST scope (HttpServletRequest object).

    Objects can be stored in SESSION scope using the setAttribute(String name, Object value) method and retrieved using the getAttribute(String name) method on the HttpSession object.

    Sessions have a configurable timeout period and can be explicitly invalidated using the invalidate() method.
  4. APPLICATION Scope
    Objects stored in APPLICATION scope are shared among all servlets and users within the same web application. This makes it suitable for storing application-wide configuration data, shared resources, or cached data that should be available to all users.

    Since APPLICATION scope objects are shared across multiple threads, proper synchronization is essential when modifying these objects to ensure thread safety.

    The APPLICATION scope is unique per web application per JVM, meaning each web application has its own separate APPLICATION scope.

    The servlet container creates an APPLICATION scope for each web application when the application is deployed and started.

    The APPLICATION scope is represented by an instance of the ServletContext class.

    The ServletContext can be obtained by calling getServletContext() method from within a servlet, or getServletContext() method on the HttpSession object.

    Objects can be stored using the setAttribute(String name, Object value) method and retrieved using the getAttribute(String name) method on the ServletContext object.
© 2025 mtitek