[ Team LiB ] 10.6 Special Events In

[ Team LiB ] 10.6 Special Events In the bean sense, special events refer to a couple of event types that are of particular interest to bean authors working with JSPs. The first is called PropertyChange Event. A bean may fire one of these any time one of its properties changes, in order to alert other beans to the change. A property that generates a PropertyChangeEvent when it is modified is called a bound property. A bean can also refuse to set a property to a new value, by generating a VetoEvent. This is typically thrown when another object tries to set a property to an unacceptable value. The inventory bean might throw this exception if someone tried to change the number of items it is holding to 1. A property that can generate a VetoEvent is known as a constrained property. These two events are defined in the bean specification. The JSP specification defines an additional event, HttpSessionBindingEvent, which can be used to notify a bean that it has been added to or removed from a session scope. Recall that a session will end after a user has not come back to the site after a certain length of time. When this happens, the session will be deleted to make room in memory for other sessions, and any data in the session will be lost. However, before the session is killed, all data objects connected to it will be sent an HttpSessionBindingEvent, which gives these data objects a chance to save data to a database or file or to do any other cleanup. Listing 10.9 shows a bean that saves itself when the session shuts down. Listing 10.9 A bean that listens for session binding events package com.awl.jspblog.ch10; import java.io.*; import java.util.*; import javax.servlet.http.*; public class SessionBean implements Serializable, HttpSessionBindingListener { private String fileName; private String message; public void setFileName(String fileName) { this.fileName = fileName; } public String getFileName() {return fileName;} public void setMessage(String message) { this.message = message; } public String getMessage() {return message;} public void valueBound(HttpSessionBindingEvent b) { } public void valueUnbound(HttpSessionBindingEvent b) { save(); } Page 193

Hint: This post is supported by Gama web hosting php mysql provider

Comments are closed.