• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpSessionListener

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!");
}
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank,
Thanks again for your help and patience.
I'll create a WAR file and put it into Webapps tomorrow.

-- Mike
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic