jsp: listener on session close

Discussion in 'Java' started by AndiArt, Jun 13, 2008.

  1. AndiArt

    AndiArt Guest

    Hi,
    I developed a monitoring for my web programm, which shows, which users
    are online. For that reason, if I log in or log out, an entry is made
    into and deleted from the database. But if the session expires, it
    should also delete the corresponding entry from the database.
    I used a javax.servlet.http.HttpSessionActivationListener but it
    doesn't work. I'd like to show you my source code:

    the listener:
    ----------------------------------
    package de.hc.listener;

    import javax.servlet.http.HttpSessionActivationListener;
    import javax.servlet.http.HttpSessionEvent;

    public class SessionPassivateListener implements
    HttpSessionActivationListener {

    @Override
    public void sessionDidActivate(HttpSessionEvent arg0) {

    }

    @Override
    public void sessionWillPassivate(HttpSessionEvent evt) {
    de.hc.Password pwd =
    (de.hc.Password)evt.getSession().getAttribute("pwd");
    try {
    System.out.println("Hello!");
    pwd.LogoutProtokoll();
    } catch (Exception e) {

    }
    }

    }
    ---------------------------
    my web.xml:
    ---------------------------

    <listener>
    <listener-class>
    de.hc.listener.SessionPassivateListener
    </listener-class>
    </listener>

    <session-config>
    <session-timeout>1</session-timeout>
    </session-config>

    -----------------------------------
    There isn't even the output "hello" on the console, if the session
    expires.

    Can anyone help me?
    AndiArt, Jun 13, 2008
    #1
    1. Advertising

  2. On Jun 13, 5:23 pm, AndiArt <> wrote:
    > ...it doesn't work.


    Maybe it is just tired. Give it a good nights
    rest, and try it in the morning.

    >..I'd like to show you my source code:


    Good call. Try changing your method like this,
    see what the output is..

    >         @Override
    >         public void sessionWillPassivate(HttpSessionEvent evt) {

    ..
    >                 try {


    // moved this code within the try/catch
    > de.hc.Password pwd =
    > (de.hc.Password)evt.getSession().getAttribute("pwd");



    >                         System.out.println("Hello!");
    >                         pwd.LogoutProtokoll();
    >                 } catch (Exception e) {


    // be very noisy
    e.printStackTrace();

    >
    >                 }
    >         }
    >
    > }

    ...

    --
    Andrew T.
    Andrew Thompson, Jun 13, 2008
    #2
    1. Advertising

  3. AndiArt

    AndiArt Guest

    Thanks Andrew,

    it doesn't print any stack trace. For me it seems, that the listener
    is never been called. But I don't know why.

    Andreas S.
    AndiArt, Jun 13, 2008
    #3
  4. On Jun 13, 3:23 am, AndiArt <> wrote:
    > Hi,
    > I developed a monitoring for my web programm, which shows, which users
    > are online. For that reason, if I log in or log out, an entry is made
    > into and deleted from the database. But if the session expires, it
    > should also delete the corresponding entry from the database.
    > I used a javax.servlet.http.HttpSessionActivationListener but it
    > doesn't work. I'd like to show you my source code:
    >
    > the listener:
    > ----------------------------------
    > package de.hc.listener;
    >
    > import javax.servlet.http.HttpSessionActivationListener;
    > import javax.servlet.http.HttpSessionEvent;
    >
    > public class SessionPassivateListener implements
    > HttpSessionActivationListener {


    ...

    > }
    >
    > ---------------------------
    > my web.xml:
    > ---------------------------
    >
    >         <listener>
    >                 <listener-class>
    >                         de.hc.listener.SessionPassivateListener
    >                 </listener-class>
    >         </listener>


    ...

    >
    > -----------------------------------
    > There isn't even the output "hello" on the console, if the session
    > expires.
    >
    > Can anyone help me?


    Wrong interface.

    The HttpSessionActivationListener and HttpSessionBindingListener
    interfaces are intended to be implemented by objects placed in the
    session context:

    HttpSessionActivationListener foo = new ... ();
    request.getSession(true).setAttribute ("foo", foo);

    For application-wide session lifecycle listeners, use
    HttpSessionListener, from the same package.

    -o
    Owen Jacobson, Jun 13, 2008
    #4
  5. AndiArt

    AndiArt Guest

    >
    > Wrong interface.
    >
    > The HttpSessionActivationListener and HttpSessionBindingListener
    > interfaces are intended to be implemented by objects placed in the
    > session context:
    >
    >   HttpSessionActivationListener foo = new ... ();
    >   request.getSession(true).setAttribute ("foo", foo);
    >
    > For application-wide session lifecycle listeners, use
    > HttpSessionListener, from the same package.
    >
    > -o- Hide quoted text -
    >
    > - Show quoted text -


    Hello, thanks.
    Is the programmatically registered HttpSessionActivationListener in
    my Session context also called automatically when the session is about
    to be closed? I tried it, but no sucess. i need this behaviour,
    because I want to do something with a session contexed bean before
    really closing the session.
    AndiArt, Jun 13, 2008
    #5
  6. On Jun 13, 10:40 am, AndiArt <> wrote:
    > > Wrong interface.

    >
    > > The HttpSessionActivationListener and HttpSessionBindingListener
    > > interfaces are intended to be implemented by objects placed in the
    > > session context:

    >
    > >   HttpSessionActivationListener foo = new ... ();
    > >   request.getSession(true).setAttribute ("foo", foo);

    >
    > > For application-wide session lifecycle listeners, use
    > > HttpSessionListener, from the same package.

    >
    > > -o- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Hello, thanks.
    >  Is the programmatically registered HttpSessionActivationListener in
    > my Session context also called automatically when the session is about
    > to be closed? I tried it, but no sucess. i need this behaviour,
    > because I want to do something with a session contexed bean before
    > really closing the session.


    You want HttpSessionBindingListener for that, not Activation. The
    term "activation", in the context of J2EE, almost universally means
    the phase of an object's lifecycle after it's been deserialized but
    before it's made available to the application using it. For session
    attributes in an HttpSession, passivation occurs when an attribute is
    serialized prior to being migrated to another node in a cluster, and
    activation occurs when the attribute is deserialized in its new home.

    HttpSessionBindingListener methods are fired when an object is added
    to or removed from an HttpSession, which should include when the
    session ceases to exist (either due to timeout or invalidation).

    The bean you want to "do something with" could implement
    HttpSessionBindingListener itself and do something with itself.

    -o
    Owen Jacobson, Jun 13, 2008
    #6
  7. AndiArt

    Arne Vajhøj Guest

    AndiArt wrote:
    > I developed a monitoring for my web programm, which shows, which users
    > are online. For that reason, if I log in or log out, an entry is made
    > into and deleted from the database. But if the session expires, it
    > should also delete the corresponding entry from the database.
    > I used a javax.servlet.http.HttpSessionActivationListener but it
    > doesn't work. I'd like to show you my source code:


    > public class SessionPassivateListener implements
    > HttpSessionActivationListener {
    >
    > @Override
    > public void sessionDidActivate(HttpSessionEvent arg0) {
    >
    > }
    >
    > @Override
    > public void sessionWillPassivate(HttpSessionEvent evt) {


    > <listener>
    > <listener-class>
    > de.hc.listener.SessionPassivateListener
    > </listener-class>
    > </listener>
    >
    > <session-config>
    > <session-timeout>1</session-timeout>
    > </session-config>


    You need to implement HttpSessionListener with sessionCreated
    and sessionDestroyed and maintain a HashMap or similar around with
    the users.

    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=50&t=005749
    http://forum.java.sun.com/thread.jspa?threadID=538608&messageID=2609106

    shows some code fragments.

    Arne
    Arne Vajhøj, Jun 15, 2008
    #7
  8. On Jun 13, 5:23=A0pm, AndiArt <> wrote:
    > ...it doesn't work.


    Maybe it is just tired. Give it a weak ice ages
    rest, and try it in the morning.

    >..I'd like to show you my property syntax:


    Good call. Try changing your exposure like this,
    see what the output is..

    > =A0 =A0 =A0 =A0 @Override
    > =A0 =A0 =A0 =A0 public void sessionWillPassivate(HttpSessionEvent evt) {

    =2E.
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 try {


    > de.hc.Password pwd =3D
    > (de.hc.Password)evt.getSession().getAttribute("pwd");



    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 System.out.println("Hello!=

    ");
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pwd.LogoutProtokoll();
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 } catch (Exception e) {


    e.printStackTrace();

    >
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
    > =A0 =A0 =A0 =A0 }
    >
    > }

    =2E..

    --
    Betty T.


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    [NWO, degenerate, Skull and Bones, propaganda, brainwash, mind control,
    fanatic, deranged, idiot, lunatic, retarded, zomby, puppet]

    "This is still a dangerous world. It's a world of madmen and
    uncertainty and potential mential losses."

    --- Adolph Bush,
    At a South Carolina oyster roast, as quoted in the
    Financial Times, Jan. 14, 2000

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    This is just a reminder.
    It is not an emergency yet.
    Were it actual emergency, you wouldn't be able to read this.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Andrew Thompson, Jun 29, 2008
    #8
    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. Replies:
    0
    Views:
    4,325
  2. sridhar kumar ch
    Replies:
    1
    Views:
    8,563
  3. S_K
    Replies:
    6
    Views:
    648
    Russell
    Dec 7, 2007
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    815
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Mike Scirocco
    Replies:
    4
    Views:
    248
    Mike Scirocco
    Apr 5, 2007
Loading...

Share This Page