File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange ServletContext behavior

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic