The DeltaSpike Servlet module propagates a number of Servlet object
lifecycle events to the CDI event bus. This allows regular CDI beans to
observe these events and react accordingly.
In most cases the event type is the object whose lifecycle is observed.
To distinguish between construction and destruction of the corresponding
object, DeltaSpike uses the qualifiers @Initialized
and @Destroyed
.
The following sections shows which concrete Servlet objects are
supported and how their lifecycle can be observed.
Servlet Context Lifecycle Events
The Servlet module supports initialization and destruction events for
the ServletContext
. These events can for example be used to detect
application startup or shutdown. The following code shows how these
events can be observed:
public void onCreate(@Observes @Initialized ServletContext context) {
System.out.println("Initialized ServletContext: " + context.getServletContextName());
}
public void onDestroy(@Observes @Destroyed ServletContext context) {
System.out.println("Destroyed ServletContext: " + context.getServletContextName());
}
The events are emitted from a ServletContextListener
called
EventBridgeContextListener
. You can disable lifecycle events for the
ServletContext
by deactivating the following class:
org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener
If you manually registered the required filters and listeners, you can
also simply remove the entry for the EventBridgeContextListener
from
your web.xml
to disable the events.
Request and Response Lifecycle Events
The Servlet module also supports initialization and destruction events
for the HttpServletRequest
and HttpServletResponse
. These events can
for example be used for initialization work like invoking
setCharacterEncoding
on the request.
The following example shows how to observe lifecycle events for the
request:
public void onCreate(@Observes @Initialized HttpServletRequest request) {
System.out.println("Starting to process request for: " + request.getRequestURI());
}
public void onDestroy(@Observes @Destroyed HttpServletRequest request) {
System.out.println("Finished processing request for: " + request.getRequestURI());
}
Observing lifecycle events for the response works the same way:
public void onCreate(@Observes @Initialized HttpServletResponse response) {
System.out.println("HttpServletResponse created");
}
public void onDestroy(@Observes @Destroyed HttpServletResponse response) {
System.out.println("HttpServletResponse destroyed");
}
All events of this category are emitted from a servlet filter called
EventBridgeFilter
. If you want to disable events for this category,
just use DeltaSpike’s deactivation mechanism to deactivate the following
class:
org.apache.deltaspike.servlet.impl.event.EventBridgeFilter
If you manually registered the required filters and listeners you can
also simply remove the entry for the EventBridgeFilter
from your
web.xml
to disable the events.
Session Lifecycle Events
The last category of events supported by the DeltaSpike Servlet module
are the lifecycle events for the user’s HTTP session. The following
example shows how these events can be observed from a regular CDI bean.
public void onCreate(@Observes @Initialized HttpSession session) {
System.out.println("Session created: " + session.getId());
}
public void onDestroy(@Observes @Destroyed HttpSession session) {
System.out.println("Session destroyed: " + session.getId());
}
The lifecycle events for the HTTP session are sent from a
HttpSessionListener
called EventBridgeSessionListener
. To disable
this event category, deactivate the following class:
org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener
If you manually registered the required filters and listeners you can
also simply remove the entry for the EventBridgeSessionListener
from
your web.xml
to disable the events.