wood burning stoves 2.0*
The moose likes JSP and the fly likes HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "HttpSessionListener" Watch "HttpSessionListener" New topic
Author

HttpSessionListener

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I've created a Servlet that implements HttpSessionListener and I've overridden the two required methods (see below). All these two overriden methods should do for the moment is write a string to the log when a session is created or destroyed.
However, I can create multiple sessions, but nothing is being written to the log.
The examples I have seen in books of the HttpSessionListener "register" the listeners in the web.xml file, but I'm implementing this application without a web.xml file (just installing it in the WEB-INF\classes directories). Does this make any difference?
Any ideas why this code isn't correctly binding to the JSP pages when they create/destory a session?
Thanks much in advance!
-- Mike

//***************************************************
public void sessionCreated(HttpSessionEvent event)
//***************************************************
{
System.out.println("Session Created!");
}
//*****************************************************
public void sessionDestroyed(HttpSessionEvent event)
//*****************************************************
{
System.out.println("Session Destroyed!");
}
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You say I'm implementing this application without a web.xml file (just installing it in the WEB-INF\classes directories). Does this make any difference?
The bottom line is that, yes, it does make a difference. If you don't tell the container where to send the session events, it won't send you any. Just because you have made a class which implements an interface does not imply that the container will magically know which object to send events to.
As a general rule, you almost always need a web.xml file for anything more complicated than simple JSPs with inline code fragments. As soon as you need to pass initialization parameters, or map servlets to URLs, or register listeners, or apply filters, or define an authentication (login) process or anything else which involves linking together multiple bits of an application, you need a web.xml file.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Hi Frank,
Thanks again for your help and patience.
I'll create a WAR file and put it into Webapps tomorrow.

-- Mike
Ken Robinson
Ranch Hand

Joined: Dec 23, 2003
Posts: 101
I would not recommend creating a servlet that implements HttpSessionListener.
Servlets and HttpSessionListeners are two different animals, each having their own seperate unrelated tags in web.xml.
When you register a servlet with the <servlet> tag, the container will create one instance of that servlet and not use any non-servlet methods, unless you call them directly in your code.
When you register an HttpSessionListener (or any other Life Cycle Listener), the container creates an instance of the specified class and only calls it's Listener methods when appropriate.
If you use the same class, what you will get is two instances of the same class with one only being used by the container as a servlet and one only as a listener. In each object, half the method/members may not be used at all. Seperate the logic into two seperate classes.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Kenneth is right. And I am no expert, but just to add that you should implement the HttpSessionListener on classes that you instantiate from Servlets and place in a Session. Something like a UserBean as I discussed in your other post.
Others will correct me if they don't agree.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSessionListener