• 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

servletContext

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm designing a class that is used by a servlet and it needs a reference to the servlet context for resource access. However, I don't have a reference to either the servlet or its context. Is it possible to get this from the container via JNDI lookup? If so is there an example out there? If not, is there any alternative to passing the context in?
Thanks in advance
Jeff Gaer
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you pass a reference to the ServletContext or the calling Servlet in this class's constructor?
 
Jeff Gaer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could, but the architecture doesn't allow for that. To the best of my knowledge the servlet context is only available from the servletConfig that is passed to the servlet at initialization, but before I contradict the architect I want to make sure that is a correct assumption.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if your class will be used by a servlet in its init() method you could get the ServletContext object from the ServletConfig object. You can refer to servlet docs. You could consider preloading your servlet.
 
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
The GenericServlet class, which HttpServlet extends, has methods to get you the ServletContext and ServletConfig any time.
getServletConfig() and getServletContext()
Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic