jsp: listener on session close

A

AndiArt

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>
 
A

Andrew Thompson

...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();
                }
        }

}
...
 
A

AndiArt

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.
 
O

Owen Jacobson

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
 
A

AndiArt

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.
 
O

Owen Jacobson

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
 
A

Arne Vajhøj

AndiArt said:
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
 
A

Andrew Thompson

...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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top