aspose file tools*
The moose likes Servlets and the fly likes ServletContext AttributeListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext AttributeListener" Watch "ServletContext AttributeListener" New topic
Author

ServletContext AttributeListener

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi there,
I'm trying to get the ServletContext AttributeListener (this is 1 word, but Javaranch doesn't like words more than 30 letters long, hence the space) to work.
I've been using a servlet which implementing this.
However none of the 3 methods...
attributeAdded,attributeRemoved,attributeReplaced
seem to be invoked when I use the code:

and then change the value using:

I'm using netbeans 3.5, I don't know if I have to change anything
in the deployment desciptor or not.

Please can some one show me a simple servlet which implements this Listener
,showing how one of these methods(eg attributeReplaced) works, perhaps by incrementing an integer that is a class variable.

Thanks for any help or comments.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

I don't know if I have to change anything in the deployment desciptor or not.


You need to establish the class as a listener in the deployment descriptor. How else is the container to know that you want it to be a listener?
[ December 13, 2005: Message edited by: Bear Bibeault ]

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

Joined: Apr 11, 2005
Posts: 744
Thanks, but I've tried that, with the listener at the top,
this is basically it.



Does that look right?
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67



Where's the package part of the class name?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I didn't put it in a package, I just put it in the classes directory of WEB-INF.
Does it definatley need to be in a package?
I'm always told packages are a good idea, but didn't think it was a mandatory requirement in this situation.
[ December 13, 2005: Message edited by: colin shuker ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

Originally posted by colin shuker:
I didn't put it in a package, I just put it in the classes directory of WEB-INF.


Put it in one.


Does it definatley need to be in a package?


Yes.

I'm always told packages are a good idea, but didn't think it was a mandatory requirement in this situation.


Yes they are. Yes it is.
[ December 13, 2005: Message edited by: Bear Bibeault ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes, it needs to be in a package. And, you were informed correctly...using packages is a very good idea, and in some cases absolutely neccessary.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Conise answers and straight to the point, I like it.
I'll try it out.
Thanks
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I tried using packages, but It still didn't work.
Maybe I'm using setAttribute in the wrong way, I'm basically calling
it several times in the doGet method.
Heres my code if you want to see.
The class is called ch.chserv


And here is the web.xml file


Thanks again for any help, its much appreciated
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

I'm betting that the listener IS working -- just not the way you are expecting. I'll write up the explanation when I'm done with dinner.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Excellent, thanks very much!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

The problem is that you've ganged up the servlet and the listener into one class. Not only is this an odd thing to do, it's not going to work the way you intend.

The container will create one instance of the class as a listener, and then yet another instance as the servlet. So when your servlet performs a setAttribute(), the listener instance is notified. Therefore the num instance in the servlet instance is never changed.

So to fix your problem, separate the disparate functions into separate classes: one servlet, and one listener.

I also have the following unrelated recommendations:

  • Use standard Java naming conventions. It's surprisingly hard to read Java code that does not. Take for example, you servlet chserv. You should uppercase the first letter of each word. Also, why make me guess what ch stands for? Charley? Name the servlet something like CharleyServlet.
  • As dicsussed, always put classes in a package other than the default.
  • Never ever ever, and did I mention ever, use instance variables in a servlet. I realize that in this case you were trying to use it as a communication mechanism between the listener portion of the class and the servlet portion, but doing so is just asking to get hit with threading issues.
  • Why use a servlet to emit markup when a JSP is so much better suited for it? (The converse is also true: processing should be done in a servlet, not a JSP).



  • [ December 13, 2005: Message edited by: Bear Bibeault ]
    colin shuker
    Ranch Hand

    Joined: Apr 11, 2005
    Posts: 744
    Thanks for the advice, maybe I can explain a little better what I'm
    trying to do now that I've thought about it.
    I want to create a chat application, similar to yahoo messenger, but just
    very basic.
    My plan is to use 2 servlets, and a bean.
    Call the first servlet Display.java
    This is going to display 10 lines of text say, and underneath, will
    be a space for Text Input, (with name="line"), alongside a SUBMIT button.
    Upon clicking submit, the sentence written in the box will retrieved in
    the second servlet(Data.java) by using

    Now in this second servlet (Data.java), a bean (scope="application")
    will be updated, basically as:

    beans.Bean.line1=beans.Bean.line2;
    beans.Bean.line2=beans.Bean.line3;
    beans.Bean.line3=beans.Bean.line4;
    beans.Bean.line4=beans.Bean.line5;
    beans.Bean.line5=beans.Bean.line6;
    beans.Bean.line6=beans.Bean.line7;
    beans.Bean.line7=beans.Bean.line8;
    beans.Bean.line8=beans.Bean.line9;
    beans.Bean.line9=beans.Bean.line10;
    beans.Bean.line10= newLine;

    Then control will be forwarded back to Display.java, and the entered text should be displayed on screen.

    Here is the problem with the listener, when a 2nd user submits a line of text, the 10 lines of text will be updated, but the 1st users screen will need to be refreshed to see the new text, which is why I wanted to use a ServletContext AttributeListener to listener out for changes to context attributes (ie, the bean) so that the Display.java page can be redisplayed.

    Hope that makes some sense
    Please let me know if you think I can implement this listener in Display.java, I don't think I can going on from what u said in last post.
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026

    Does it definatley need to be in a package?

    Yes.


    Why do we have to put the classes in packages. Can't the container read the classes from WEB-INF/classes folder.


    Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Vishnu Prakash:


    Why do we have to put the classes in packages. Can't the container read the classes from WEB-INF/classes folder.


    See section 8, Bullet 2 in this page.
    http://java.sun.com/j2se/1.4/compatibility.html#incompatibilities1.4


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026

    See section 8, Bullet 2 in this page.


    Sorry Ben I couldn't locate what you had said. can you paste it over here.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Vishnu Prakash:


    Sorry Ben I couldn't locate what you had said. can you paste it over here.


    Sun's site is absolutely exausting.
    I swear that link pointed to the right page yesterday.
    I'll find the correct page and post it.
    It may be later today.
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    I thought that the default package problem was only with tomcat. I am eagerly waiting to read the link. :roll:


    Groovy
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    [OFF TOPIC]


    Originally posted by Pradip Bhat:
    I thought that the default package problem was only with tomcat. I am eagerly waiting to read the link. :roll:


    No, it's with Java.
    Unfortunately, Sun's site seems to be in a state of flux (as if it's ever been anything else). That link which laid out changes between 1.4 and 1.3 now redirects you back to a general page which states:


    The compatibility documents are divided to track incompatibility only between adjacent versions. For example, this 1.4.2 compatibility page details only 1.4.2 incompatibility with 1.4.1 and not previous versions. Therefore, to find how 1.4.2 is incompatible with all versions, you would need to look on all compatibility pages.


    They then give you 5 links to the pages that show the incompatiblities among prior versions. Yesterday, these links worked. Today, they just redirect you back to the current page.
    Frustrating!

    Colin,
    Sorry about the off topic posts.
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Ben,

    Is the link available now?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ServletContext AttributeListener