*
The moose likes Servlets and the fly likes ServletContext outside the Servlet Container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext outside the Servlet Container" Watch "ServletContext outside the Servlet Container" New topic
Author

ServletContext outside the Servlet Container

samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Hi Ranchers,
Is it possible to get the hold over ServletContext object outside the servlet container. I want to make the use of servletcontext in my POJO code to get some information stoired in this object. Is it possible to do that ? . Any link, white paper will be greatly appriciated.
Thanks in advance
Samir
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Bad idea. You could pass a reference to the context to the POJO, but now that ties your POJO to the servlet environment and gives it access that it probably should not have. That's just plain poor design.

Rather, why does your POJO need this? Answer that and you could get a better way to accomplish your actual goal.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Hi Bear
Thanks for the prompt reply. I have decleared some variables in web.xml which I are accessible through the ServletContext ,and I want to keep some custom object variable in sink with these values. These custom object I dont want to expose to servlet . If someone changes the values in web.xml , my custom object variable value also need to be changed. Writting a code of accessing the ServletContext in the POJO could suffice my need or otherwise please let me know the alternative solution for the same .
Thanks
Samir
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Pass the values, not the servlet context, to the POJO.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi,
While reading the post I had some questions so just thought of posting it.
1) If POJO wants to get some values from ServletContext , I assume that POJO is loaded as part of some servlet call in webapp class loader. This would mean POJO executing inside Servlet Container itself. In this case why would you say ServletContext outside the Servlet Container.
2) As per Bear's reply passing the values and not the ServletContext to the POJO would be a good design with regards to coupling between the POJO and Servlet APIs. This would enable re-use of POJO in some other framework.
Please correct me if I am wrong, especially in #1.

Regds,
Amit
 
Consider Paul's rocket mass heater.
 
subject: ServletContext outside the Servlet Container
 
Similar Threads
Servlet Context Vs Servlet Config
Context
Passing variables from class to class (SOLVED)
Why does a servlet has init method
Can a request be forwarded to a resource in different web application and the same web container?