Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to get HttpSession object in servlet without HttpServletRequest object

Reply
Thread Tools

How to get HttpSession object in servlet without HttpServletRequest object

 
 
Jim
Guest
Posts: n/a
 
      08-25-2003
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


 
Reply With Quote
 
 
 
 
Wolfram Rittmeyer
Guest
Posts: n/a
 
      08-25-2003
Jim wrote:
> 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

 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      08-25-2003
Jim wrote:
> "Wolfram Rittmeyer" <(E-Mail Removed)> wrote in message
> news:bicf9d$7osje$(E-Mail Removed)-berlin.de...
>
>>Jim wrote:
>>
>>>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
>>

>
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

> Thanks again,
> jim cant
>
> Here's my doGet():
> public void doGet(HttpServletRequest request, ...
> {
> ...snip...
> if ( request.getSession().getValue( "Listener" ) == null )
> {
> request.getSession().putValue( "Listener", new
> SessionListener() );
> }
> }
>
>


 
Reply With Quote
 
Jim
Guest
Posts: n/a
 
      08-26-2003
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


"John C. Bollinger" <(E-Mail Removed)> wrote in message
news:bidfnv$om4$(E-Mail Removed)...
> Jim wrote:
> > "Wolfram Rittmeyer" <(E-Mail Removed)> wrote in message
> > news:bicf9d$7osje$(E-Mail Removed)-berlin.de...
> >
> >>Jim wrote:
> >>
> >>>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
> >>

> >
> > 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 Removed)
>
> > Thanks again,
> > jim cant
> >
> > Here's my doGet():
> > public void doGet(HttpServletRequest request, ...
> > {
> > ...snip...
> > if ( request.getSession().getValue( "Listener" ) ==

null )
> > {
> > request.getSession().putValue( "Listener", new
> > SessionListener() );
> > }
> > }
> >
> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting servlet context path without a servlet request. stuart.j.wood@gmail.com Java 3 06-06-2006 06:38 AM
Getting the HTTPSession of a SOAP servlet? Scott Balmos Java 0 09-12-2005 11:40 PM
Obtain HttpServletRequest object in WebService Eric Chan Java 1 06-23-2004 10:49 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Browser and HttpSession Object Tim Java 3 09-03-2003 08:27 PM



Advertisments