Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext AttributeListener

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where's the package part of the class name?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conise answers and straight to the point, I like it.
I'll try it out.
Thanks
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, thanks very much!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 750
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1026
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Vishnu Prakash
    Ranch Hand
    Posts: 1026
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought that the default package problem was only with tomcat. I am eagerly waiting to read the link. :roll:
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ben,

    Is the link available now?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic