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 	HttpSessionListener doesn't listens to the event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "	HttpSessionListener doesn Watch "	HttpSessionListener doesn New topic
Author

HttpSessionListener doesn't listens to the event

Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Hi I am arun.The HttpSessionListener event is not firing up when a new session is created.The following program(ConcurrentUserTracker) increments the static variable "users" once when each session is created.
The servlet DispalyUsers displays the number of current active sessions in a webapplication.But the output I am always getting 0(zero) eventhough more than 5 sessions accesses the practice(Web Application Name).See I have localarea network thats why I can able to access the webapplication simultaneously with 5 sessions.

Web Application name is practice



Here is the servlet code which displays the number of active sessions.




and finally here is the listener element which I added to the deployment descriptor web.xml of the webapplication(practice)



I added this element immediately after the <web-app> element.

then the <servlet> and <servlet-mapping> elements are deployed as follows




after this



After performing all this operations I opened my browser and executed the servlet I got the output as

Users:0

I fighted a lot to get the output as Users:1 or more but I don't.So please someone help me to resolve this problem

[ July 28, 2006: Message edited by: Arun Somasundaram ]


[BPSouther: Fixed subject line to match original post]
[ July 29, 2006: Message edited by: Ben Souther ]

Arun.V.Somasundaram
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Before discussing your problem, you should put your servlets and listeners into packages.


[My Blog]
All roads lead to JavaRanch
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Mr I have all my class file and xml configuration files in the myapplication/WEB-INF/classes directory.It is not neccessary to have the servlet class files in a separate package inside the web application during development.
[ August 01, 2006: Message edited by: Arun Somasundaram ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You don't have any code that would cause a sessin to be created.

Add the following line to your servlet:


You may also want to throw a JSP in the root directory with the following code (for testing):
kill.jsp


Also, it is true that in this case, you don't need to have your classes packaged but it is a real bad idea not to do so; especially if you're trying to debug an issue. Starting in 1.4.1, Sun's JVM tightened up on a part of the spec that disallows the importing of classes in the default package (no stated package) from classes not in the default package. With servlets and listeners, this isn't a problem, with beans imported into JSPs it is.
Rather than try to understand when it's OK and when it's not OK, it makes sense to me (again: Especially when trying to debug something) to make sure all classes are packaged.

I would have, at least, tried packaging those two classes before dismissing Satou's suggestion.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
You don't have any code that would cause a sessin to be created.---->is wrong

Mr.Ben Souther
you are wrong , bco'z you dont want to create a HttpSession for this code.First know one thing.The container automatically creates a unique session for a client when he requests for some resource in the container regardless you get a reference to the session object.You are getting a reference to the session object created by the container.

Be clear with the follwing points:-

The HttpSessionListener Interface provides notification of two events:

1.)session creation
2.)session destruction


The methods required by this interface are

1.) void sessionCreated(HttpSessionEvent evt)

The method is invoked when a session is created by the container.This method is almost always be invoked only once per a unique client.

2.) void sessionDestroyed(HttpSessionEvent evt)

The method is invoked when a session is destroyed by the container.This method is invoked when a unique client's session is timed out, that is after they fail to revisit the web site for a given period of time usually 15 minutes.



and I have implemented those two methods in my ConcurrentUserTracker class.So the respective methods will be invoked by the container whenever a new session is created or destroyed.So the static variable users will be incremented or decremented.This is the logic behind my program.

There is no need to create a session in the code.

[ July 29, 2006: Message edited by: Arun Somasundaram ]
[ July 29, 2006: Message edited by: Arun Somasundaram ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

OK, that's twice that you've dismissed a suggestion without trying it.

In this case, you're wrong.
Before posting my answer to your question, I ran your code both with and without my suggestion.
Without my changes, the listener's methods were not called. With them, they were.

Why? Because the container doesn't create the session until it is needed.
Look at:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html#getSession()
and
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html#getSession(boolean)
As I said before, there is nothing wrong with the way you've implemented your listener.
It's working fine.
You're not seeing the expected results because there is no code in your servlet that would cause the container to create a session.


Here is a URL to a war file containing my suggestion.
http://www.souther.us/session-test.war


If you already know the answers to your questions, why are you aksing them here?
[ July 29, 2006: Message edited by: Ben Souther ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
It is not neccessary to have the servlet class files in a separate package inside the web application.

My distilled experience from years of working with servlets and helping people figure out bugs and odd behavior with servlets is that putting all classes involved with servlets in a package is a good idea.
Now, if you want to ignore that advice, fine, but don't expect any sympathy from me
Bill
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Ok Mr.Ben Souther it is said that the container creates a unique session when a client requests a resource in a web application.I am not oppsing you,what I want to know is why the method is not invoked when a new session is created,more over the container automatically creates the session implicitly.We are just getting the reference to the session created by the container using request.getSession() method. The container invokes the method

void sessionCreated(SessionEvent evt)


and a session is created when a client accesses the webapp for the first time(the session time out is 15 minutes as we know it is default.If the client doesnot visits the webapp for 15 minutes the session is timed out. so a new session wont be created for the client until the old session is destroyed or timed out.) independent of weather we are creating a session using request.getSession() method.I agree to your explanation,but the method void sessionCreated(SessionEvent evt) should be invoked independent of request.getSession().

I Dont know the answer for my problem thats why I am asking the professional here

[ July 29, 2006: Message edited by: Arun Somasundaram ]
[ July 29, 2006: Message edited by: Arun Somasundaram ]
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
HttpSessionListener

Refer this URL you will get a clear Idea.There it is not said that the class which implements the HttpSessionListener interface should use request.getSession() to get a reference to the session created.I the respective methods are invoked then what is the purpose of the events in JAVA.
[ July 29, 2006: Message edited by: Arun Somasundaram ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Session listeners don't create sessions or cause them to be created.
Their existence does not affect whether a session will be created or not.

They merely do something if and when a session is created or destroyed.
In the case of your code, they did nothing becuase the only servlet in your app (DisplayUsers) did not have any code that would cause the container to create one. Merely making a request to the app doesn't guarantee that a session object will be created. Look at the API for the getSession methods I gave you earlier.

Adding a call to getSession to your doGet method will cause the server to create a session (if one does not already exist). When this happens, the sessionCreated method in your listener will be called by the container.
You could also create any "hello, world" JSP that doens't have the session="false" directive in it and that will cause a session to be created (by using the same technique that I've added to your servlet).

I have to be honest with you here...
I'm not very interested in continuing this debate with you if you aren't willing to try the solutions I'm offering.
I have handed you code (as a complete and runnable war file) that demonstrates what I've said, quite clearly. I've also provided links to the the relavent API methods which explain this.
If you run my example and it doesn't work, then I'm willing to stick it out.
If you run it and it works but you still can't understand why, then I'm glad to stick it out.
If, however, you are only going to keep telling me and others why we're wrong, without even trying our suggestions, then I'm finished with this thread.
Of course, I won't interrupt the discussion (unless someone isn't being nice); I just won't participate.
[ July 29, 2006: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Arun Somasundaram:
HttpSessionListener

Refer this URL you will get a clear Idea.There it is not said that the class which implements the HttpSessionListener interface should use request.getSession() to get a reference to the session created.I the respective methods are invoked then what is the purpose of the events in JAVA.

[ July 29, 2006: Message edited by: Arun Somasundaram ]


In the example I posted, and gave to you, I added the call to getSession to the servlet; not the listener.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The sentence in bold is not entirely correct and may be the source of your confusion:
Ok Mr.Ben Souther it is said that the container creates a unique session when a client requests a resource in a web application.I am not oppsing you,what I want to know is why the method is not invoked when a new session is created,more over the container automatically creates the session implicitly. We are just getting the reference to the session created by the container using request.getSession() method. The container invokes the method


From the link to the API method getSession(boolean) that I posted earlier:

Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.


You are partially correct.
Yes getSession returns a handle to the session but, it also has the side effect of triggering the container to create one if one doesn't already exist.

It was logical for the spec designers to do this because it eliminates the overhead of session creation if they aren't needed.

If, as you said earlier, the container always and automatically creates a session when the first request made to the application, there would be no need for getSession(boolean) and the API for getSession() would only state that the method returns a reference to the session object for this request.

Since your servlet never asked the container for a reference to the session object, one was never created. By making call to getSession(), the code that I added caused the container to create one and (as you will see if you try it) caused the sessionCreated method to be called.
[ July 29, 2006: Message edited by: Ben Souther ]
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Thanks Mr.Ben Souther.I agree to you.Thanks for giving a lot of suggestions and even a code(war file) which works fine.You are so patient to make me understand the concept and I really appreciate you and finally again thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working for you.
Welcome to Javaranch.
Romi Dave
Greenhorn

Joined: Jul 19, 2006
Posts: 26
This was a nice discussion to read. But now can you tell me how can i know which session is created , I mean how can i get the session id immediately when the session is created.

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HttpSession has a getId method.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionEvent.html#getSession()
 
jQuery in Action, 2nd edition
 
subject: HttpSessionListener doesn't listens to the event
 
Similar Threads
HttpSessionListener doesn't listens to the event
setmaxInactiveInterval
ListenerTester
doubt in HTTPSessionListener implementation?
Servlet Listeners--not working