Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return a specific session attribute from HttpSessionListener

 
g forte
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have a HttpSessionListener that works fine.
I can get it to return a active session count and also each session ID.


Now I tried to return a specific attribute by using the following code:


but it returns null.
In my jsp I set it like so


Is this expected bahaviour?
If not, how can I return a listing of all sessions.getAttribute("myAttribute")

TIA!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When does your JSP execute in relation to the listener mothods?
 
g forte
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure as to when the listener is called in relation to the jsp. I have the listener registered in web.xml and I get the System.out results almost exactly when I load the jsp which only contains the following code for testing.


Is there a way to control when the listener is called?

Perhaps this is not the correct design pattern for what I am trying to accomplish....my thoughts were that this would be a way to generate a listing of all users that are logged into a chat room.
Ideas?
Thanks for your input.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by g forte:
Not sure as to when the listener is called in relation to the jsp.


Then depending upon the JSP to create the scoped variable isn't a very good idea, is it?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me you would get better results by adding the
HttpSessionAttributeListener interface to MySessionListener
because it has methods that get called when your code does a setAttribute()

Bill
 
g forte
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
not sure I understand you though.
Is it not possible to return an attribute from the listener or is this poor design or both?

Do you have any ideas as to a way to track people when they are in the chat room.

I figured that if I return the getAttribute("userName") and added it to a Application scoped variable (array,List,etc...) I could just print this variable to the page on each subsequent ajax call. Then when the sessionDestroyed method is called I could remove this userName from the variable.
Make sense?

Thanks again.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by g forte:

not sure I understand you though.


Your listener depends upon the JSP to set the scoped variable. Yet what assurances to you have the the JSP has ever been called?

Your design is poor because your listener depends upon the action of a JSP that it has no idea has ever executed or not.

So, how could you break that dependency?
[ July 18, 2006: Message edited by: Bear Bibeault ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The sessionCreated method is called when the session object is created, now when do you think that happens?
Answer: It happens BEFORE any of your session related code executes - when the servlet engine creates it before handing a reference to your code.
THEREFORE it can't possibly see attributes you set after getting the session instance.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic