Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»

The HttpSessionAttributeListener thus provides very similar functionality to HttpSessionBindingListener. As already suggested, the primary difference is that HttpSessionBindingListener must be implemented directly by objects being added to the session, whereas classes implementing HttpSessionAttributeListener are not direct participants in session activity. The HttpSessionAttributeListener interface can thus be considered noninvasive, because it allows session activity to be monitored without needing to modify the objects involved in that session activity. Implementing HttpSessionBindingListener, however, requires access to the source code for the object whose session activity is to be monitored.

NOTE Direct access to the source code of a class to which you wish to add HttpSessionBindingListener support is not strictly required. You can always create a subclass of the original class that inherits the methods of the original and also implements the HttpSessionBindingListener interface. Alternatively, the delegation design pattern can be used to create a class implementing HttpSessionBindingListener, which incorporates an instance of the original class as one of its instance variables. The methods of HttpSessionBindingListener are implemented directly by this new class, while those of the original class are forwarded (i.e., delegated) to the object stored in this instance variable.

The choice of which listener to use is application-dependent. If a given object is generic and intended for use in a wide range of applications (i.e., not necessarily

web-related) then HttpSessionAttributeListener may be more appropriate. By doing so, the session-related functionality can be isolated in a separate class that need only be included when the object is used in a web-based application. If the object is specifically geared toward deployment with servlets and JSP pages, then having its class also implement HttpSessionBindingListener may be preferred. This approach is particularly appropriate if the object needs to track session activation and passivation as well, since this will require it to also implement HttpSes-sionActivationListener.

Скачать в pdf «Web Development with JavaServer Pages»