<< Chapter < Page Chapter >> Page >
Listing 9 - The inner class named MyClass.
class MyClass implements HttpSessionBindingListener, Serializable{PrintWriter localOut;//local copy of output stream to clientpublic MyClass(PrintWriter out){//constructor //Save a local copy of the output stream to the client.localOut = out; }//end constructorpublic String toString(){ return "This is a MyClass object";}//end toString()//This method is called when the object is put into // the session.public void valueBound(HttpSessionBindingEvent e){ localOut.println("<p>Event<br/>"); localOut.println("In valueBound method<br/>"); //Returns the name of the object as identified when// put into the session localOut.println("Name = " +e.getName() + "</p>"); }//end valueBound()//This method is called when the object is removed // from the session.public void valueUnbound(HttpSessionBindingEvent e){ localOut.println("<p>Event<br/>"); localOut.println("In valueUnbound method<br/>"); localOut.println("Name = " +e.getName() + "</p>"); }//end valueUnbound()}//end inner class named MyClass}//end class Java4580a

Deploying the servlet

When this program is compiled, it produces the following two class files:

  • Java4580a.class
  • Java4580a$MyClass.class

The second class file results from the compilation of the member class.

I mention this as a reminder that it is necessary to copy both of these class files in order to deploy the servlet onto a web server. Note however that whendeploying on my local Tomcat server, only the information about the top level class must be entered into the file named web.xml .

A MyClass object

An object of the class named MyClass is instantiated and put into the session object during the first call to the servlet when the hit counter equals one. (See Listing 6 .) The object is removed during the fourth call to the servlet when the hit counter equals four. (Again, see Listing 6 .)

A listener object

Because the class implements the HttpSessionBindingListener interface, an object of the class is a listener for events of type HttpSessionBindingEvent . An event of this type occurs when the object is put into the session object. Another event of this type occurs when theobject is removed from the session object.

Event handler methods

The method named valueBound is called when the object is put into the session object. The method named valueUnbound is called when the object is removed from the session object.

No registration necessary

Unlike typical Java event programming, it is not necessary to register the listener on a source. Simply implementing the HttpSessionBindingListener interface is sufficient to cause the object to be notified of the events when an object of the class is put intoor removed from the session object.

An object of type HttpSessionBindingEvent

When an event occurs, the event handler methods named valueBound and valueUnbound receive a reference to an object of type HttpSessionBindingEvent as a parameter.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask