Run:
| EventHandler

View:
| globals.jsa
| index.jsp

###

### Event Handlers Sample

This sample illustrates using a globals.jsa file to process applications and session lifecycle events. The sample counts the number of active sessions, the total number of sessions, and the total number of times the application's page has been hit. Each of these values are maintained at the application scope. The application's page (index.jsp) updates the page hit count on each request. The globals.jsa's session_OnStart event handler increments the number of activeSessions and the total number of sessions. The globals.jsa's session_OnEnd handler decrements the number of active sessions by one.

The page output is simple. When a new session starts the session counters are output. The page counter is output on every request. The final tally of each value is output in the globals.jsa's application_OnEnd handler.

Note: Many servers don't provide the ability to manually shut down an application. Those that do don't recognize the globals.jsa as an application marker. To see this sample in action you can cause the application to be automatically shutdown and restarted by touching or updating either index.jsp or globals.jsa. The Oracle JSP engine always terminates a running application before retranslating and reloading an active page.

Other things to notice in this sample:

1. When updating the counter variables access must be synchronzied as these values are maintained at application scope.

2. The count values use the Oracle JSP data type bean oracle.jsp.jml.JmlNumber. This is a built-in bean that makes working with simple data values at scope easy. You will learn more about these Bean types in chapter 5.