• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

servletContext

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic