How to get HttpSession object in servlet without HttpServletRequest object

J

Jim

I want to get a hold of the HttpSession object in a servlet but only way
I've seen
to get this is through a request object and these only seem exposed in the
doGet() or doPost() methods.

Is there another way to get the session?

I want to attached a HttpSessionBindingListener but don't want to check to
see if it is there (and attach it if not) every time doGet() or doPost() is
called.

Any advice?

I am prematurely thankful,

jim cant
 
W

Wolfram Rittmeyer

Jim said:
I want to get a hold of the HttpSession object in a servlet but only way
I've seen
to get this is through a request object and these only seem exposed in the
doGet() or doPost() methods.

Is there another way to get the session?

I want to attached a HttpSessionBindingListener but don't want to check to
see if it is there (and attach it if not) every time doGet() or doPost() is
called.

Any advice?

I am prematurely thankful,

jim cant
I'm not sure that I understand your problem. The attributes (i.e.
objects) you are storing in (or binding to) your session via its
setAttribute-method can implement the HttpSessionBindingListener to get
notified of themselves getting bound/unbound to any session. From within
this object's "valueBound"-Method you can get the session using the
HttpSessionBindingEvent passed to that method. Your servlet doesn't even
need to know about this. So why exactly do you need the session from
within your servlet? Furthermore you should use servlets only for
request/response-tasks. Therefore you should indeed use doPost(),
doGet() or any of the other standard methods which provide a
request-object anyway.

I think there must be something else to your qeustion I missed
completely. Maybe you could add some informations?

Wolfram Rittmeyer
http://www.jsptutorial.org
 
J

John C. Bollinger

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)
 
J

Jim

In response to John's comments (copied from below)
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.

I was using the HttpSessionBindingListener interface attaching a object
implenting this
to the session object do I could catch the destruction of a session when it
timed out.

Your suggestion of the HttpSessionListener is what I really wanted since
this is where one
can directly get notified of invocations of session creation and
destruction. If I can manage to
from the 'scope' of the 'servlet container' back to the scope of the web-app
so that I can let
the user know his session has gone away, I'll be all set. (any hints on
that? can it be done?)

Thanks for the help.

jim cant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top