<< Chapter < Page | Chapter >> Page > |
Listing 6 - An object of type MyClass. |
---|
if(cnt.intValue() == 1)
session.putValue("MyClassObj", new MyClass(out));if(cnt.intValue() == 4) session.removeValue("MyClassObj"); |
Also as shown in Listing 6 , this object is removed from the session object when the value of the hit counter is 4.
An HttpSessionBindingEvent event happens when the object is put into the session object and happens again when the object is removed fromthe session object.
Event handling
The object of type MyClass is a listener for events of type HttpSessionBindingEvent . Therefore, it receives an event notification when it is put into the session object, andreceives another event notification when it is removed from the session object. Information about the two events is displayed on the client screen when the events occur.
The text at the top of Figure 1 was produced when the MyClass object was put into the session. The text at the top of Figure 4 was produced when the object was removed from the session. More information about that textwill be provided later during the discussion of the class named MyClass .
Display information about the session
The code in Listing 7 displays several pieces of information about the session each time the servlet is called. This information is obtained from thesession object by calling various methods on the session object. The information is then mixed with standard HTML code and displayed on the client screen asshown in Figure 1 through Figure 7 .
Listing 7 - Display information about the session. |
---|
out.println("<p>Session Characteristics:<br/>");
out.println("New Session: " + session.isNew()+ "<br/>");
out.println("Session ID: " + session.getId()+ "<br/>");
out.println("Session Context: "+ session.getSessionContext()+ "<br/>");
out.println("Creation Time: "+ new Date(session.getCreationTime()) + "<br/>");
out.println("Last Accessed: "+ new Date(session.getLastAccessedTime()) + "</p>"); |
Display information about the objects
The code in Listing 8 displays information about each of the objects stored in the session object each time the servlet is called. Note that the order ofthe display of objects doesn't seem to follow any particular pattern.
Listing 8 - Display information about the objects. |
---|
out.println("<p>Session Data:<br/>");
String[]names = session.getValueNames();
for(int i = 0; i<names.length; i++){
out.println(names[i]+ ": "
+ session.getValue(names[i]) + "<br/>");
}//end for loop//Finish off the HTML page
out.println("</p></body></html>");
}//end doGet() |
The code in Listing 8 calls the getValueNames method to create a String array containing the names of each of the objects currently stored in the session object. A for loop then iterates on that array, calling the getValue method on each name to get and display each of the objects stored in the session object.
(The code in Listing 8 also finishes off the web page and signals the end of the doGet method.)
The information that is displayed
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?