• Post Reply Bookmark Topic Watch Topic
  • New Topic

servletContext

 
Jeff Gaer
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
 
Anthony Villanueva
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!