ADF Listener - Header

ADF Application Event listeners – Part I

Hi all and welcome to a new series on Red Mavericks!

This one is technical, focused on Oracle Application Development Framework, or ADF, so… sorry to our Business Analyst readers. There’s a new article coming out to new next week about Advanced Web Form creation… stay tuned.

Anyway, today’s article is written by a new Maverick: Pedro Gabriel. Welcome Pedro!

Pedro is a senior ADF developer at Link Consulting, and he’ll be sharing some of his experience with our fellow techie readers. He would be very interested in hearing back from you through twitter @PedrohnGabriel. Be sure to follow him  and check his Bio, right here🙂

So, without further ado, here is Pedro’s article.


ADF applications provide a wide range of Event Listeners triggered whenever a change in state occurs. With these event listeners you are able to interact with:

  • The state of the servlet context
  • The state of the servlet request
  • The HTTP session object
  • Servlet Filters
  • Phase Listeners

 

Servlet Context Events / Servlet Request Events

This type of events involves resources or state held at the level of the application servlet context/request object. You have four types of servlet context events:

 

Event Type Interface Methods Description
Servlet Context Listener javax.servlet.ServletContextListener contextInitialized() Servlet context is created.
contextDestroyed() Servlet context is about to be destroyed.
Servlet Context Attributes Listener javax.servlet.ServletContextAttribuestListener attributeAdded() An attribute is added to the Servlet Context Listener.
attributeRemoved() An attribute is removed from the Servlet Attribute Listener.
attributeReplaced() An attribute is replaced from the Servlet Context Listener.
Servlet Request Listener javax.servlet.ServletRequestListener requestInitialized() The request is about to come into scope of the web application.
requestDestroyed() The request is about to go out of scope of the web application.
Servlet Request Attributes listener javax.servlet.ServletRequestAttribuestListener attributeAdded() A new attribute was added to the servlet request. Called after the attribute is added.
attributeRemoved() A new attribute was removed from the servlet request. Called after the attribute is removed.
attributeReplaced() A new attribute was replaced from the servlet request. Called after the attribute is replaced.

 

From the moment the ADF application is invoked until it is totally rendered for usage by the end user the events are triggered in the following order:

ServletEventsOrder

In order to listen and interact to any of these event listeners you need to create a new servlet context or request listener class and implement the previous mentioned interfaces. The next image shows how to create it.

CustomServletContext

Then you need to register them in the “web.xml” file in order to listen and interact to the events for the servlet context or request listeners. Set the custom event listener class path. For now on when you run the ADF application the filter will be triggered.

CustomServletContext_Web_xml

HTTP Session Events

This type of events involves resources or state associated with series of requests from a single user session that is associated with the HTTP session object. You have two types of http session events:

 

Event Type Interface Methods Description
HTTP Session Listener javax.servlet.http.HttpSessionListener sessionCreated() An HTTP session is activated.
sessionDestroyed() An HTTP session is about to be passivated.
HTTP Session Attribute Listener vavax.servlet.http.HttpSessionAttributeListener attributeAdded() An attribute is added to the HTTP session listener.
attributeRemoved() An attribute is removed from the HTTP session listener.
attributeReplaced() An attribute is replaced from the HTTP session Listener.

 

The HTTP session events are executed in the following order:

HttpEventsOrder

To listen and interact to any of these event listeners you need to create a new HTTP listener class and implement the previous mentioned interfaces. The next image shows how to create it.

HttpSessionListener

Then you need to register them in the “web.xml” file in order to listen and interact to the events for the HTTP event listeners. Set the custom event listener class path. For now on when you run the ADF application the filter will be triggered.

HttpSessionListener_WebXml

Servlet Filters

Servlet filters are used for pre-processing Web application requests and post-processing responses. Sometimes modifying the response from a class of servlets during pre-processing might be useful. You can have as much filters as you want and are invoked based on the order they are configured in the web.xml file.

When you create a new filter class it must implement the “javax.servlet.Filter” interface. The next image shows how to create it.

CustomFilter

You will find your custom filter implementing 3 methods:

  • init – filter initialization and saves the filter configuration in a variable.
  • doFilter – filter implementation. You can check whether the filter is called during a request or a response.
  • destroy – filter finalization. You can override this method to perform extra actions.

After filter creation you must register it on the “web.xml” file in order to listen and interact to the events for the filter.

CustomFilter_Register

Phase Listeners

ADF lifecycle integrates with JSF request lifecycle. By using a phase listener, you can listen to the events fired before and after for any of the ADF lifecycle phases, and execute custom code. Take a look at the JSF and ADF lifecycles in the next image so you can understand the range of events you can listen:

adf_jsf_lifecycle

In order to listen and interact to ADF lifecycle phases you need to create a new phase listener class and implement the “javax.faces.event.PhaseListener” interface. The next image shows how to create it.

CustomPhaseListener

Then you need to register it in the “faces-config.xml” file in order to listen and interact to the events. Set the custom phase event listener class path.

CustomPhaseListener_FacesConfig

Conclusion

By creating your own event listeners classes you are able to listen and interact with a wide range and variety of data that is triggered whenever you perform any action in your ADF application.

On the next post we’ll provide some examples regarding the type of data that you can find and intercept in the event listeners.

Cheers,

Pedro Gabriel

 

@PedrohnGabriel

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *