Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get HttpSessionListener to respond?

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran through the example in K&B on using ServletContextListener, that works fine.

But then I tried to use a HttpSessionListener to respond to a user's invoking a servlet by putting an attribute into the session. This listener never gets called.

My listener:



The DD:



The servlet, which I expected would trigger the session listener:



I expected that line 03 above would trigger the session listener to create the attribute. But the output is "Your dog is null". So I am not quite understanding how sessions work.

Am I going about this wrong? Or is this something I should not try at all?
 
Kris Schneider
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Kennedy wrote:But the output is "Your dog is null".


Hm. Well, if you didn't get a NullPointerException by referencing d.toString()...what does the SessionDog.toString method look like?
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, that's it! I was populating the breed member but not the name member. Fixed!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic