File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Strange ServletContext behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Strange ServletContext behavior" Watch "Strange ServletContext behavior" New topic
Author

Strange ServletContext behavior

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have a Servlet that I load on startup of Tomcat. I use this servlet to simply load some ResourceBundles to use throughout the application.
So what I do in the init() is:

And then in another Serlvet I do the following:

When this code gets executed, I get a NullPointerException. Am I doing this right/wrong??


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Is the 'get' also in an init()? If so, it's possible you've run into a race condition.
(Along the same lines, are you pre-loading the servlet that does the 'set' at startup?)
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

The 'get' is NOT in an init method. It is in a method called processRequest which gets called from both doGet and doPost.
Yes, I am pre-loading the servlet. I have System.out command in my init method (for testing just this) and I can see it printing out when I start Tomcat before I ever access my application.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, I got this to work. But I am not sure why I had to do it this way.
I built an "Action" class that is abstract. It looks like:

Now, originally, I was not passing the ServletConfig to the processRequest method. But now that I am and I am using:

This NullPointer error message have gone away and everything is working just fine.
Once again, I have no idea why I have to pass the ServletConfig around to get at the ServletContext.
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
That doesn't make sense to me.
The HttpServlet getServletContext method actually returns getServletConfig().getServletContext(), so the ServletContext reference you get should be the same, regardless of how it is called.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Chad McGowan:
That doesn't make sense to me.
The HttpServlet getServletContext method actually returns getServletConfig().getServletContext(), so the ServletContext reference you get should be the same, regardless of how it is called.

That's what I thought, but it wasn't doing what it was supposed to be doing. At least I have a work around until I figure out what the heck is going on.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12778
    
    5
If any of your servlets are addressed using the "invoker" syntax, that could explain why they are not sharing the same servlet context.
If that does not explain it - maybe you better post the web.xml.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange ServletContext behavior