File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes ServletContext AttributeListener problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext AttributeListener problem" Watch "ServletContext AttributeListener problem" New topic
Author

ServletContext AttributeListener problem

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi there, I'm trying to get this servlet to change a JLabel's text, when an attribute is added to the servlet context.

You can see in the doGet(...) method, that an attribute is added, and that the servlet implements ServletContext AttributeListener.(should be one word, but is too long to be allowed here)
Also, the web.xml file contains...

But when the servlet is run, the JLabel never changes, I'm not even sure if the public void attributeAdded(ServletContextAttributeEvent scab) method is executing, I don't think I can put a simple System.out.println(...) in there.




Here is the servlet code

Any ideas?
Thanks
[ February 04, 2007: Message edited by: colin shuker ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

JLabel? Servlet? What on earth are you doing?

You do realize that by running Swing from a servlet that the display can only appear on the server, right?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
I never really thought about, but I guess so now.
But still, on my pc, the JLabel doesn't change, I don't why.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
I never really thought about, but I guess so now.
But still, on my pc, the JLabel doesn't change, I don't why.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

From your other post I assume that you have ascertained that the listener methods are not triggering?

But that doesn't explain why you are running a Swing program out of a servlet. How odd.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi again, can anyone explain why the code below doesn't execute the
attributeAdded(...) method



The servlet is included in the web.xml file under <listener> tag.
Thanks for any help

PS. I was using the swing thing as a means of testing the listener, but what I've done is put a form in the out.println statements, and then reprinted the attribute value from another servlet, and it still hasn't changed, so the attributeAdded() method can't have been called, and the listener is definately present in the web.xml, can any one see whats going wrong.

Thanks

[ February 04, 2007: Message edited by: colin shuker ]
[ February 04, 2007: Message edited by: colin shuker ]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Well I've made some progress, I created a class and let it implement ServletContextAttributeListener, and got this to write to a file inside the attributeAdded() method.

Then I made the original servlet NOT implement the listener, and I just made an object of this new class in the servlet before setting the ContextAttribute.

I ran program and looked at the file, and it had been written to, confirming the Listener worked when implemeneted by a normal java class.

So my question is... can servlets implement ServletContextAttributeListener, or is best left to normal classes?

Thanks
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
This time... I changed back, to the code 2 posts up, but instead of setting a new attribute value in the attributeAdded() method, I wrote to a file instead, and it actually worked.

So that means that the addedAttribute method works fine when a servlet implents ServletContextAttributeListener, but for some reason it wouldn't let me modify the attributes value.

I should be able to figure it out myself now.
But thanks for everyones valued comments
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Well I've figured something out...
In the servlet (that implements ServletContextAttributeListener),

When I use...

This throws an exception, which is why nothing was working before leading me to the false assumption that the listener didn't work.

So now... why does it throw an exception? That code would work fine in doGet(), but not here?

Any ideas?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Do you not think that it could be helpful to post the details of the exception?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
I appreciate the advise, but the patronising comments aren't necessary.
I didn't have time last night to find out about the exception, so I just posted what I'd found, hoping there was an obvious solution to why it was throwing an exception, but I will look into it now.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

No patronization intended, just a straight-forward question. Posting "it doesn't work" without details just isn't very helpful.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
I didn't say it didn't work, I said the code

throws an Exception.

I've just worked out its a NullPointerException, so I'm guessing theres a problem with this.getServletContext(), but I don't see why it shouldn't work in this method
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

I was being generic. More specifically, saying "it throws an exception" without the details of the exception is usually not helpful.

You may wish to investigate the API for ServletContextAttributeEvent to discover the proper way to access the servlet context. Remember that you are implementing a Listener interface in a servlet (not the wisest thing to do) so it has no knowledge about the servlet-ness of the object.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
OK, I checked the API and I've fixed it...

Since ServletContextAttributeEvent extends ServletContextEvent which contains the method getServletContext().

But is it strongly advisable not to implement a servlet with ServletContextAttributeListener?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Originally posted by colin shuker:
This throws an exception...
Aha. And that would explain why the infinite loop I was expecting didn't occur. But that itself would have thrown an exception, a stack overflow exception.
[ February 05, 2007: Message edited by: Paul Clapham ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Originally posted by colin shuker:
But is it strongly advisable not to implement a servlet with ServletContextAttributeListener?


Yes, because it can get you into trouble with things like NPE's when your listener code does things that depend on the class being a servlet, and vice versa.

Since the listener code can't assume that it's in a servlet, and vice versa, there's no utility in mixing up the listener and servlet code into the same class.

So why do it and introduce the possiblity of screwing things up and getting weird behavior such as NPEs or worse?
Debasish Ray Chawdhuri
Greenhorn

Joined: Feb 05, 2007
Posts: 24
well i registered just yesterday. i think its very simple, when you use the same class as a listener and add it in web.xml, it creates an object of the class for listening to events, and another for serving request as servlet.
The object used for listening is kept in a different environment than a servlet and thus the getServletContext cannot obtain the reference to the servlet context of the web-app


SCJP 1.5, SCWCD 1.4, SCBCD 5.0 It's the attitude, and not the aptitude, that determine one's altitude in life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext AttributeListener problem
 
Similar Threads
ServletContextAttribute Listener doesnot works with Vector object change
ServletContext AttributeListener
My example with ServletContextListener and ServletContextAttrib.
ServletContext AttributeListener again
ServletContext events