aspose file tools*
The moose likes Servlets and the fly likes ServletContextListener not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextListener not working" Watch "ServletContextListener not working" New topic
Author

ServletContextListener not working

Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
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

Joined: Jan 10, 2002
Posts: 61239
    
  66

Place the class in a package other than the default. This has been a recording.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
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

Joined: Jan 10, 2002
Posts: 61239
    
  66

So what have you done to debug it? Have you ascertained that the context listener is firing?
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Apr 05, 2010
Posts: 96
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

Joined: May 04, 2011
Posts: 144

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?


OCJP6, OCWCD5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61239
    
  66

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

Joined: Apr 05, 2010
Posts: 96
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

Joined: Jan 10, 2002
Posts: 61239
    
  66

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

Joined: Apr 05, 2010
Posts: 96
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

Joined: Jan 10, 2002
Posts: 61239
    
  66

No. What value is being passed as the first argument to the setAttribute() method?
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
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

Joined: Jan 10, 2002
Posts: 61239
    
  66

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

Joined: Apr 05, 2010
Posts: 96
yes. Thanks again for all your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContextListener not working