File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes return a specific session attribute from HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "return a specific session attribute from HttpSessionListener" Watch "return a specific session attribute from HttpSessionListener" New topic
Author

return a specific session attribute from HttpSessionListener

g forte
Greenhorn

Joined: Aug 28, 2003
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61103
    
  66

When does your JSP execute in relation to the listener mothods?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
g forte
Greenhorn

Joined: Aug 28, 2003
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Aug 28, 2003
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Mar 22, 2000
Posts: 12769
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return a specific session attribute from HttpSessionListener