Home

sessionscoped

SessionScoped is a scope annotation used in Java EE's Contexts and Dependency Injection (CDI) and in JavaServer Faces (JSF) to define the lifecycle of a bean. In CDI, a bean annotated with @SessionScoped has its lifecycle bound to the HTTP session. A single instance is created for a user’s session when the bean is first needed and is reused for all subsequent requests within that session. The bean persists through multiple requests and pages for the same user, until the HTTP session ends due to logout, invalidation, or timeout.

Across different user sessions, separate bean instances exist. This makes session scope suitable for maintaining user-specific

In CDI deployments, session-scoped beans are typically required to be Serializable to support possible passivation or

In JSF, there is also a legacy annotation to define session scope: javax.faces.bean.SessionScoped. While it serves

state
across
pages
and
requests,
such
as
a
shopping
cart,
user
preferences,
or
temporary
form
data
during
a
logged-in
session.
The
scope
is
tied
to
the
session
lifecycle,
so
the
bean
is
destroyed
when
the
session
ends.
replication
in
clustered
environments.
If
the
container
passivates
sessions,
the
bean
should
implement
Serializable
to
avoid
runtime
issues.
a
similar
purpose
for
JSF
managed
beans,
using
CDI’s
@SessionScoped
is
generally
preferred
in
modern
applications
to
leverage
CDI
features,
dependency
injection,
and
better
interoperability
with
other
CDI
scopes
and
contexts.
The
two
annotations
belong
to
different
ecosystems
and
have
distinct
lifecycles
and
injection
semantics.