• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange ServletContext behavior

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic