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
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.
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 .
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.
subject: ServletContext outside the Servlet Container