Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Context without servlet

 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to get the Servlet or application context for a class(which is not a servlet) running in web container ??
 
Jasmine kaur
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well to get context of servlet you can use method getServletContext() which will return u the servlet context .
See the servelt documentation as this method is available in every servlet class so u can use it and see , I hope this would work.
Thanks
Jasbir
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if its running in a web container one would think it is called from a servlet, in which case you can pass the context object from the servlet to the other classes?
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to do that. It is called from servlet/JSP's but I want the class to be independent of that. i.e the class should be able to query something and get the servlet context
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a reference to ServletConfig in your class, you can use it to get the ServletContext.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is called from servlet/JSP's but I want the class to be independent of that. i.e the class should be able to query something and get the servlet context

Exactly what do you need from the servlet context? Perhaps there is a better way to get it.
It seems to me your requirements are incompatible - time for a redesign maybe. If you can make the class independent of the servlet environment, it will be easier to test.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic