SeamFramework.orgCommunity Documentation
By including the Seam 3 Servlet module in your web application (and performing the necessary listener configuration for pre-Servlet 3.0 environments) you will also have the servlet lifecycle events propagated to the CDI event bridge so you can observe them in your beans. The event bridge works by installing a servlet listener and firing events on the BeanManager with associated qualifiers, passing along the event object.
These events correspond to the javax.servlet.ServletContextListener
interface.
The event object fired is a javax.servlet.ServletContext
(since it's the
only relevant information in the javax.servlet.ServletContextEvent
object). There are
two qualifiers available that can be used for selecting the initialization or destruction of
the servlet context.
Qualifier | Description |
---|---|
@Initialized | Qualifies the creation event |
@Destroyed | Qualifies the destruction event |
If you want to listen to both lifecycle events, leave out the qualifiers:
public void observeServletContext(@Observes ServletContext ctx)
{
// Do something with the "servlet context" object
}
If you are interested in only a particular one, use a qualifer:
public void observeServletContextInitialized(@Observes @Initialized ServletContext ctx)
{
// Do something with the "servlet context" object upon initialization
}
The name of the observer method is insignificant.
The ServletContext
initialized event provides an excellent opportunity to perform startup
logic as an alterative to using an EJB 3.1 startup singleton. Even better, you can configure the bean to be
destroyed immediately following the initialization routine by leaving it as dependent scoped (dependent-scoped
observers only live for the duration of the observe method invocation).
Here's an example of entering seed data into the database in a development environment (as indicated by a
stereotype annotation named @Development
).
@Stateless
@Development
public class SeedDataImporter
{
@PersistenceContext
private EntityManager em;
public void loadData(@Observes @Initialized ServletContext ctx)
{
em.persist(new Product(1, "Black Hole", 100.0));
}
}
If you'd rather not tie yourself to the Servlet API, you can observe the WebApplication
type
instead, which is an informational object provided by Seam Servlet that holds select information about the
ServletContext
such as the application name, context path, server info and start time.
public void loadData(@Observes @Initialized WebApplication webapp)
{
...
}
These events correspond to the
javax.servlet.ServletRequestListener
inteface. The event object fired is a
javax.servlet.ServletRequest
(since it's the only relevant information in
the javax.servlet.ServletRequestEvent
object. There are two qualifiers
available that can be used for selecting the initialization or destruction of the request.
Qualifier | Description |
---|---|
@Initialized | Qualifies the initialization event |
@Destroyed | Qualifies the destruction event |
If you want to listen to both lifecycle events, leave out the qualifiers.
public void observeRequest(@Observes ServletRequest request)
{
// Do something with the servlet "request" object
}
If you are interested in only a particular one, use a qualifer
public void observeRequestInitialized(@Observes @Initialized ServletRequest request)
{
// Do something with the servlet "request" object upon initialization
}
You can also listen specifically for a javax.servlet.http.HttpServletRequest
simply by changing the expected event type.
public void observeRequestInitialized(@Observes @Initialized HttpServletRequest request)
{
// Do something with the HTTP servlet "request" object upon initialization
}
The name of the observer method is insignificant.
The Servlet API does not provide a listener for accessing the lifecycle of a response. Therefore, Seam Servlet
simulates a response lifecycle listener using CDI events. These events parallel those provided by the
javax.servlet.ServletRequestListener
inteface. The event object fired is a
javax.servlet.ServletResponse
. There are two qualifiers available that can be used for
selecting the initialization or destruction of the response.
Qualifier | Description |
---|---|
@Initialized | Qualifies the initialization event |
@Destroyed | Qualifies the destruction event |
If you want to listen to both lifecycle events, leave out the qualifiers.
public void observeResponse(@Observes ServletResponse response)
{
// Do something with the servlet "response" object
}
If you are interested in only a particular one, use a qualifer
public void observeResponseInitialized(@Observes @Initialized ServletResponse response)
{
// Do something with the servlet "response" object upon initialization
}
You can also listen specifically for a javax.servlet.http.HttpServletResponse
simply by changing the expected event type.
public void observeResponseInitialized(@Observes @Initialized HttpServletResponse response)
{
// Do something with the HTTP servlet "response" object upon initialization
}
The name of the observer method is insignificant.
These events correspond to the javax.servlet.HttpSessionListener
interface. The event
object fired is a javax.servlet.http.HttpSession
(since it's the only relevant information
in the javax.servlet.http.HttpSessionEvent
object). There are two qualifiers available that
can be used for selecting the initialization or destruction of the session.
Qualifier | Description |
---|---|
@Initialized | Qualifies the creation event |
@Destroyed | Qualifies the destruction event |
If you want to listen to both lifecycle events, leave out the qualifiers. Note that omitting all qualifiers
will observe all events with a HttpSession
as event object.
public void observeSession(@Observes HttpSession session)
{
// Do something with the "session" object
}
If you are interested in only a particular one, use a qualifer
public void observeSessionInitialized(@Observes @Initialized HttpSession session)
{
// Do something with the "session" object upon being initialized
}
The name of the observer method is insignificant.
These events correspond to the
javax.servlet.HttpSessionActivationListener
interface. The event object
fired is a javax.servlet.http.HttpSession
(since it's the only relevant
information in the javax.servlet.http.HttpSessionEvent
object). There
are two qualifiers available that can be used for selecting the activation or passivation
of the session.
Qualifier | Description |
---|---|
@DidActivate | Qualifies the activation event |
@WillPassivate | Qualifies the passivation event |
If you want to listen to both lifecycle events, leave out the qualifiers. Note that omitting all qualifiers will
observe all events with a HttpSession
as event object.
public void observeSession(@Observes HttpSession session)
{
// Do something with the "session" object
}
If you are interested in only a particular one, use a qualifer
public void observeSessionCreated(@Observes @WillPassivate HttpSession session)
{
// Do something with the "session" object when it's being passivated
}
The name of the observer method is insignificant.