You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Juanjo Cuadrado <jj...@gmail.com> on 2006/10/11 14:09:51 UTC

Problem with HttpSessionListener

Hi,

    I'm trying to create a listener in my application. I have created a
listener class that implements HttpSessionLister and I have put the element
<listener>.... in my web.xml.

    My listener class only sets a object in the session
(getSession().getAttribute("k", "kk")), but when I try to recover this
object from a class (a servlet), not exist in session.

    Any idea?

P.D.
    I'm using NetBeans 5.0 and its envebed Tomcat 5.5.9

Re: Problem with HttpSessionListener

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Juan,

> public class HttpTestListener implements HttpSessionListener {
> 
>    public void sessionCreated(HttpSessionEvent e){
>        e.getSession().setAttribute("test", new String("testValue");
>    }

Okay. I assume that there's an extra ")" end the end, otherwise your
code doesn't compile. I'll chalk that up to a copy-paste error.

>    <listener>
>        <listener-class>com.test.HttpTestListener</listener-class>
>    </listener>

Okay, that looks good.

> And, finally, in a servlet I try to recover the value of "test":
> ...
> String test = (String)request.getSession().getAttribute("test");
> ...
> 
> After this, the value of de variable test is null....
> 
> What have I to do if I want to recover the value of test?

I'm guessing that you are never setting that value.

Try changing your code to this:

    public void sessionCreated(HttpSessionEvent e){
        System.out.println("sessionCreated!!!");

        e.getSession().setAttribute("test", new String("testValue"));
    }

...and then check your catalina.out log (or standard output log,
whatever that is). If you aren't getting these messages, then your
listener is not configured correctly.

Make sure that your web.xml is actually in WEB-INF (I have sometimes
forgotten to copy web.xml from my development directory into the Tomcat
deployment, so double-check). You might consider doing a fresh install
of your application into the Tomcat webapp directory (or re-deploy the
WAR file) to make sure that Tomcat doesn't somehow have a stale copy of
web.xml lying around. Make sure you restart Tomcat, too ;)

I hope that helps.

-chris



RE: Problem with HttpSessionListener

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: José Roberto Motta Garcia [mailto:garcia@cptec.inpe.br] 
> Subject: Re: Problem with HttpSessionListener
> 
> Do you mean a servlet?

No, just Java classes.

> Wouldn't every app instantiate its own 
> object if this class is a simple one?

Only if they do a new; you can use static fields and methods or singletons to insure the same set of objects are used for all webapps.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Problem with HttpSessionListener

Posted by José Roberto Motta Garcia <ga...@cptec.inpe.br>.
Hi chuck, do u know how to do that? It's what I'm trying to
>> do, to get a cross-app scope for customized management.
>>     
> If all you need are objects common to all web apps, classes placed in shared/lib will suffice.
>   
Do you mean a servlet? Wouldn't every app instantiate its own object if
this class is a simple one?

Tks

-- 
Jose Roberto M. Garcia
Analista de Sistemas - Grupo Banco de Dados
Fone: (12) 3186-8405   Fax: (12) 3101-2835
--
http://www.cptec.inpe.br 
http://www.inpe.br 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Problem with HttpSessionListener

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: José Roberto Motta Garcia [mailto:garcia@cptec.inpe.br] 
> Subject: Re: Problem with HttpSessionListener
> 
> "No one was asking for cross-application scoping."
> 
> Hi chuck, do u know how to do that? It's what I'm trying to 
> do, to get a cross-app scope for customized management.

If all you need are objects common to all web apps, classes placed in shared/lib will suffice.

Haven't done the following myself, but for something more extensive you might want to look at the doc for the crossContext attribute of the <Context> element:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

I would think you would also want to enable the SingleSignon valve in the conf/server.xml file.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Problem with HttpSessionListener

Posted by José Roberto Motta Garcia <ga...@cptec.inpe.br>.
"No one was asking for cross-application scoping."

Hi chuck, do u know how to do that? It's what I'm trying to do, to get a
cross-app scope for customized management.

Tks

Jose Roberto


>   
>> the proper way to access your webapps info is  
>> getServletContext() to get the context first..once you have 
>> YOUR webapps context
>> use setAttribute() to set the property
>> use getAttribute to retrieve the value of the property
>>     
>
> More irrelevancy.  The OP is trying to update session attributes, not
> context attributes.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>   

-- 
Jose Roberto M. Garcia
Analista de Sistemas - Grupo Banco de Dados
Fone: (12) 3186-8405   Fax: (12) 3101-2835
--
http://www.cptec.inpe.br 
http://www.inpe.br 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Problem with HttpSessionListener

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Martin Gainty [mailto:mgainty@hotmail.com] 
> Subject: Re: Problem with HttpSessionListener
> 
> Session information is scoped only to the current web 
> application (ServletContext), so information stored in one 
> context will not be directly visible in another. 

No one was asking for cross-application scoping.

> the proper way to access your webapps info is  
> getServletContext() to get the context first..once you have 
> YOUR webapps context
> use setAttribute() to set the property
> use getAttribute to retrieve the value of the property

More irrelevancy.  The OP is trying to update session attributes, not
context attributes.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Problem with HttpSessionListener

Posted by Martin Gainty <mg...@hotmail.com>.
Juan-

Session information is scoped only to the current web application (ServletContext), so information stored in one context will not be directly visible in another. 

the proper way to access your webapps info is  getServletContext() to get the context first..once you have YOUR webapps context
use setAttribute() to set the property
use getAttribute to retrieve the value of the property

Martin--

This e-mail communication and any attachments may contain confidential and privileged information for the use of the 
designated recipients named above. If you are not the intended recipient, you are hereby notified that you have received
this communication in error and that any review, disclosure, dissemination, distribution or copying of it or its 
contents
  ----- Original Message ----- 
  From: Juanjo Cuadrado 
  To: Tomcat Users List ; Martin Gainty 
  Sent: Wednesday, October 11, 2006 10:00 AM
  Subject: Re: Problem with HttpSessionListener


  I'm going to try to explain it better, if my English allows me.

  I have this classe:
  ...
  public class HttpTestListener implements HttpSessionListener {
      
      public void sessionCreated(HttpSessionEvent e){
          e.getSession().setAttribute("test", new String("testValue"); 
      }
  ...

  In my web.xml I have put the follow:
  ...
      <listener>
          <listener-class>com.test.HttpTestListener</listener-class>
      </listener>
  ...

  And, finally, in a servlet I try to recover the value of "test": 
  ...
  String test = (String)request.getSession().getAttribute("test");
  ...

  After this, the value of de variable test is null....

  What have I to do if I want to recover the value of test?




  2006/10/11, Martin Gainty <mg...@hotmail.com>: 
    message was cutoff

    which object are you attempting to recover?
    Servlet?
    SessionListener?
    Session? 
    SessionAttribute?

    also you will not set any object in Session thru getAttribute as this is an accessor method and not a mutator

    Martin --
    This e-mail communication and any attachments may contain confidential and privileged information for the use of the 
    designated recipients named above. If you are not the intended recipient, you are hereby notified that you have received
    this communication in error and that any review, disclosure, dissemination, distribution or copying of it or its 
    contents
    ----- Original Message -----
    From: "Juanjo Cuadrado" <jj...@gmail.com>
    To: <users@tomcat.apache.org >
    Sent: Wednesday, October 11, 2006 8:09 AM
    Subject: Problem with HttpSessionListener


    > Hi,
    >
    >    I'm trying to create a listener in my application. I have created a
    > listener class that implements HttpSessionLister and I have put the element 
    > <listener>.... in my web.xml.
    >
    >    My listener class only sets a object in the session
    > (getSession().getAttribute("k", "kk")), but when I try to recover this
    > object from a class (a servlet), not exist in session. 
    >
    >    Any idea?
    >
    > P.D.
    >    I'm using NetBeans 5.0 and its envebed Tomcat 5.5.9
    >


Re: Problem with HttpSessionListener

Posted by Juanjo Cuadrado <jj...@gmail.com>.
I'm going to try to explain it better, if my English allows me.

I have this classe:
...
public class HttpTestListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent e){
        e.getSession().setAttribute("test", new String("testValue");
    }
...

In my web.xml I have put the follow:
...
    <listener>
        <listener-class>com.test.HttpTestListener</listener-class>
    </listener>
...

And, finally, in a servlet I try to recover the value of "test":
...
String test = (String)request.getSession().getAttribute("test");
...

After this, the value of de variable test is null....

What have I to do if I want to recover the value of test?




2006/10/11, Martin Gainty <mg...@hotmail.com>:
>
> message was cutoff
>
> which object are you attempting to recover?
> Servlet?
> SessionListener?
> Session?
> SessionAttribute?
>
> also you will not set any object in Session thru getAttribute as this is
> an accessor method and not a mutator
>
> Martin --
> This e-mail communication and any attachments may contain confidential and
> privileged information for the use of the
> designated recipients named above. If you are not the intended recipient,
> you are hereby notified that you have received
> this communication in error and that any review, disclosure,
> dissemination, distribution or copying of it or its
> contents
> ----- Original Message -----
> From: "Juanjo Cuadrado" <jj...@gmail.com>
> To: <us...@tomcat.apache.org>
> Sent: Wednesday, October 11, 2006 8:09 AM
> Subject: Problem with HttpSessionListener
>
>
> > Hi,
> >
> >    I'm trying to create a listener in my application. I have created a
> > listener class that implements HttpSessionLister and I have put the
> element
> > <listener>.... in my web.xml.
> >
> >    My listener class only sets a object in the session
> > (getSession().getAttribute("k", "kk")), but when I try to recover this
> > object from a class (a servlet), not exist in session.
> >
> >    Any idea?
> >
> > P.D.
> >    I'm using NetBeans 5.0 and its envebed Tomcat 5.5.9
> >

Re: Problem with HttpSessionListener

Posted by Martin Gainty <mg...@hotmail.com>.
message was cutoff 

which object are you attempting to recover?
Servlet?
SessionListener?
Session?
SessionAttribute?

also you will not set any object in Session thru getAttribute as this is an accessor method and not a mutator

Martin --
This e-mail communication and any attachments may contain confidential and privileged information for the use of the 
designated recipients named above. If you are not the intended recipient, you are hereby notified that you have received
this communication in error and that any review, disclosure, dissemination, distribution or copying of it or its 
contents
----- Original Message ----- 
From: "Juanjo Cuadrado" <jj...@gmail.com>
To: <us...@tomcat.apache.org>
Sent: Wednesday, October 11, 2006 8:09 AM
Subject: Problem with HttpSessionListener


> Hi,
> 
>    I'm trying to create a listener in my application. I have created a
> listener class that implements HttpSessionLister and I have put the element
> <listener>.... in my web.xml.
> 
>    My listener class only sets a object in the session
> (getSession().getAttribute("k", "kk")), but when I try to recover this
> object from a class (a servlet), not exist in session.
> 
>    Any idea?
> 
> P.D.
>    I'm using NetBeans 5.0 and its envebed Tomcat 5.5.9
>