If you look at the code for HahtApplication.java and HahtSession.java, you’ll see several methods that are triggered when an application or a session starts or ends. In a Java project, you can add your own code to these six methods:
Application method
Description
onStart Called right after a new application is created, but before any pages are run.
onEnd Called just before an application is destroyed.
onSessionStart Called when a new session is created, but before any pages are run. (Called for each session. This method is called before the Session object’s onStart method.)
onSessionEnd Called just before a session is destroyed. (Called for each session. This method is called after the Session object’s onEnd method.)
When are these methods useful? In the Session object’s onStart method, you could populate a session recordset for use by subsequent pages in the session — for example, you might want to read in the values of a database table (a catalog, say) that’s used by several different pages. In the onEnd method, you might want to save the session state, so that you can restore it later if the user returns to the application.
If onStart fails, the Application Server calls onEnd and terminates the application or session without running any pages. There are two ways for onStart to fail:
• You can throw a runtime exception, such as this: throw new RuntimeException("Failed in onStart");
In this case, the Application Server calls onEnd, generates a runtime error message, and terminates the session.
• You can edit the onStart method to produce a custom diagnostic page. Generally, after producing a diagnostic page, you want to prevent any further running of the session and any further error output from the Application Server, so you should also call the Session object’s abandon method, which calls onEnd and terminates the session.
The onEnd methods are passed an ApplicationEvent or SessionEvent object. By calling this object’s getReason method, you can determine the reason for the event. For a session ending, the available reasons are:
Session method Description
onStart Called when a new session is created, but before any pages are run. (Called once, for the current session.)
onEnd Called just before a session is destroyed. (Called once, for the current session.) Note: onEnd is called even if onStart fails.
Java
Response aResponse = Haht.getResponse(); aResponse.setContentType("text/html"); aResponse.endHeaders();
aResponse.write("Exiting the session <br>"); aResponse.write("Failed in onStart <br>"); // Abandon the session
• SessionEvent.SESSION_TIMEOUT -— session timed out normally
• SessionEvent.SESSION_STOP_END — session was stopped by a call to the abandon method
• SessionEvent.SESSION_TERMINATED — session was terminated by an administrator
• SessionEvent.SESSION_START_ERROR — the onStart method threw an exception
The example that follows uses the Application object’s onSessionStart and onSessionEnd methods to update a variable that tracks active sessions. Example: onSessionStart, onSessionEnd
Suppose you want to keep track of the number of currently-active sessions in an application. At any point in the application, you want to be able to print out this information.
You might begin by creating a static variable, called nActiveSessions, in the HahtApplication class. Notice two particular methods in this class —
onSessionStart, which is called each time a session is started, and
onSessionEnd, which is called when a session times out. You can add code to those methods to increment and decrement nActiveSessions appropriately. You’ll also want to add a method to return the value of nActiveSessions. Here
Java /** * ExamplProj.HahtApplication.java **/ //////////////////////////////////////////////////////////////// // PACKAGE //////////////////////////////////////////////////////////////// package ExamplProj; /////////////////////////////////////////////////////////// // IMPORTS /////////////////////////////////////////////////////////// import com.haht.*; import com.haht.project.*; /** * HahtApplication class *
* Represents a HAHTsite web application.
* Put customizations for an application in this class. *
* This class is derived from GeneratedApplication. * GeneratedApplication is built by the HAHTsite IDE * according to your project configuration.
* * @version * @author * * @see ExamplProj.GeneratedApplication * @see com.haht.project.Application * **/
public class HahtApplication extends ExamplProj.GeneratedApplication {
/**
* Default constructor for HahtApplication **/
public HahtApplication() {
/**
* Called when a new session is being created. *
* @param anEvent Describes details about the event. *
**/
public void onSessionStart
(com.haht.project.SessionEvent anEvent) {
// As each session starts, increment total count nActiveSessions++;
}
/**
* Called right before a session is destroyed. *
* @param anEvent Describes details about the event. *
**/
public void onSessionEnd
(com.haht.project.SessionEvent anEvent) {
// As each session ends, decrement total count nActiveSessions--;
}
/**
* Called when a new application is being created. *
* @param anEvent Describes details about the event. *
**/
public void onStart(com.haht.project.ApplicationEvent anEvent)
{ }
Now you’re ready to add some in-line code to a dynamic page. The line shown below contains text and an expression calling the method you added to return the number of active sessions. It also prints the current date and time.
At [insert DateTime widget] there are
hahtApplication.getActiveSessionCount() active sessions. The generated code for a dynamic page automatically defines
hahtApplication and hahtSession to refer to the Application and Session objects, respectively, and casts them appropriately. You can use both these references anywhere in a dynamic page. For more information, see
“Application and Session references in a dynamic page” on page 92.
(By the way, if you try this example, at runtime you may notice a lag between the time a user leaves the application and the time the onSessionEnd method is called. This is due to the session timeout; see “Session timeout” on page 108.)
/**
* Called right before a application is destroyed. *
* @param anEvent Describes details about the event. *
**/
public void onEnd(com.haht.project.ApplicationEvent anEvent)
{ } /*
* Keep track of number of currently-active sessions */
private static int nActiveSessions=0; public int getActiveSessionCount() {
return nActiveSessions; }
}