How to get HttpSession object in servlet without HttpServletRequest object

Discussion in 'Java' started by Jim, Aug 25, 2003.

  1. Jim

    Jim Guest

    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
     
    Jim, Aug 25, 2003
    #1
    1. Advertising

  2. Re: How to get HttpSession object in servlet without HttpServletRequestobject

    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
     
    Wolfram Rittmeyer, Aug 25, 2003
    #2
    1. Advertising

  3. Re: How to get HttpSession object in servlet without HttpServletRequestobject

    Jim wrote:
    > "Wolfram Rittmeyer" <> wrote in message
    > news:bicf9d$7osje$-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


    > 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() );
    > }
    > }
    >
    >
     
    John C. Bollinger, Aug 25, 2003
    #3
  4. Jim

    Jim Guest

    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" <> wrote in message
    news:bidfnv$om4$...
    > Jim wrote:
    > > "Wolfram Rittmeyer" <> wrote in message
    > > news:bicf9d$7osje$-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
    >
    >
    > > 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() );
    > > }
    > > }
    > >
    > >

    >
     
    Jim, Aug 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    3
    Views:
    951
    Steve Claflin
    Sep 3, 2003
  2. circuit_breaker
    Replies:
    2
    Views:
    2,016
    Jack Jia
    Apr 4, 2004
  3. Eric Chan
    Replies:
    1
    Views:
    2,611
    Andy Fish
    Jun 23, 2004
  4. Scott Balmos
    Replies:
    0
    Views:
    792
    Scott Balmos
    Sep 13, 2005
  5. Replies:
    3
    Views:
    29,131
    Stuart Wood
    Jun 6, 2006
Loading...

Share This Page