This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See 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 ]
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.
[ February 04, 2007: Message edited by: colin shuker ] [ February 04, 2007: Message edited by: colin shuker ]
posted 11 years ago
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?
posted 11 years ago
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
posted 11 years ago
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?
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.
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.
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 ]
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.