File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextListener not working

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am trying to write a code to see a ContextListener class in action. I have a jsp, where clicking on a button will take me to a servlet. That servlet will retrieve a context attribute object and print it using out.println(). Also, the DD defines a context-param which is taken by the listener class and that parameter is used in the contextInitialised() method to convert into an object which is then stuck into the ServletContext object as an attribute (the same attribute which is later retrieved by the servlet)

However, at the end of it all, what the servlet prints is null instead of the attribute. Can you please point out where I am going wrong?

Here are the revelant codes :

The jsp



The web.xml


The servlet



The Listener class


The ColorClass.java class


I am doing this in Netbeans (incase that matters), and I am doing a 'Clean and Build' followed by a 'Run'. When I run, and click on the button in the jsp, the output I get is

you got null!!!


I have pasted only parts of the DD and jsp. There are other servlets from the same jsp that are working fine, so I guess the rest of the DD and jsp must be correct.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place the class in a package other than the default. This has been a recording.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I removed it from the default package and put it in the same package which has all the other servlets and the ColorClass.java file. It still gives me null.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what have you done to debug it? Have you ascertained that the context listener is firing?
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..I tried debugging the app. It seems something is going wrong in the ColorClass. In the listener class, in the following lines :



variable colour gets the correct value of "red", but clrcls gets null.
But I am not able to figure out what is going wrong
 
Paul Clapham
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will return null:

Why? Simply because you didn't add an attribute whose name is "colour". You did, however, add an attribute whose name is "red".
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for your reply. But I am not sure I got you. I think I added an attribute 'colour' with value "red" in the MyOwnListener.java class :


This should set an attribute 'colour' with a value "clrcls". "clrcls", in turn, is an object with a String variable whose value should be "red".
Am I missing something?
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there are a few places where the code could be breaking:
1) The ServletContextListener is simply not running: Have you verified it actually ran? If so, how did you do it?
2) Errors in the web.xml: Did you spell everything right? Do you have all the files in the proper directories? Can you show your application structure?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Souvvik Basu wrote:This should set an attribute 'colour' ...

No. It will set a scoped variable (attribute) with the key "red". How do you figure that the key will be "colour" when you are passing the value "red" to the setAttribute() method?
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I verified that the listener class is running. I have been using netbeans, and I used the debugging feature and put breakpoints in different places. That is how I got to know that

in MyOwnListener.java is setting the variable colour to "red"...as expected.
And as far as the directory structure/web.xml etc is concerned, I think it should be correct, as its netbeans which is maintaining the structure. The web.xml I have already given...that is what I am using.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Zal wrote:1) The ServletContextListener is simply not running: Have you verified it actually ran?
It has already been ascertained that the listener is executing.

2) Errors in the web.xml:
The problem is in the code (or rather the OP's understanding of the code). Not in the web.xml.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for your help. If I have understood correctly, the signature for the method is setAttribute(String name,Object value). Here 'name' is the name of the attribute and 'value' is the value for the attribute. Is that correct? Accordingly, what I have done is....bound the value of clrcls to an attribute named colour...isnt it?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. What value is being passed as the first argument to the setAttribute() method?
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh!!! got it. I am passing "red"....
thanks


What I should rather be doing is



This would set an attribute named attr with a value that is the object clrcls......am I correct now?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the first argument specifies the key. If you pass red it will be red, if you pass syuegfjhdsabfkgad it will be syuegfjhdsabfkgad.

It doesn't have to be a string literal, it can be any String variable or expression as well. Remember, this is just Java. All the same rules apply.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. Thanks again for all your help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic