Jim said:
Let me try to say it another way.
I want to put a listener on a session.
to get a session, I need a request object
to get a request object, I need to be in doPost() or doGet()
therefore, I need to run the attachement code with every
doPost/doGet (see below)
It seems preferable to be able to do the listener attachment once when the
session
is started. You are right that the servlet doesn't care about this session
housekeeping
but it needs to be in the game because there is, as far as I can see, no
other way to
get to the session.
Of course there is no other way for a servlet to obtain a session than
via a request object. Sessions and requests are inextricably bound
together -- there is no concept of a session independant of a request.
A servlet can determine the session that applies to a particular
request, but that is quite possibly different from the session that
applies to the next request or the one that applied to the previous
request, so it has to check for each session.
Does this make the problem clearer?
The main problem with your question appears to be what you mean by "put
a listener on a session". Are you trying to use the HttpSessionListener
interface? (That's not what you put in your sample code.)
HttpSessionListener doesn't work that way -- instead, you define an
implementation class in web.xml, and the servlet container takes care of
instantiating it once and notifying that one instance of each
HttpSessionEvent in that webapp.
If you are trying to do something else, then what? To what will your
listener listen? What do you expect binding it to a session will
accomplish? In general, if you want to bind an object to every session
when it is created then the way to do so is via an HttpSessionListner
implementation.
John Bollinger
(e-mail address removed)