<p>Is there any way in the Servlet API to access properties specified in web.xml (such as initialization parameters) from within a Bean or Factory class that is not associated at all with the web container?</p>

<p>For example, I'm writing a Factory class, and I'd like to include some logic within the Factory to check a hierarchy of files and configuration locations to see which if any are available to determine which implementation class to instantiate - for example, </p>

<ol>
<li>a properties file in the classpath,</li>
<li>a web.xml parameter, </li>
<li>a system property, or </li>
<li>some default logic if nothing else is available. </li>
</ol>

<p>I'd like to be able to do this without injecting any reference to <code>ServletConfig</code> or anything similiar to my Factory - the code should be able to run ok outside of a Servlet Container.</p>

<p>This might sound a little bit uncommon, but I'd like for this component I'm working on to be able to be packaged with one of our webapps, and also be versatile enough to be packaged with some of our command-line tools without requiring a new properties file just for my component - so I was hoping to piggyback on top of other configuration files such as web.xml.</p>

<p>If I recall correctly, .NET has something like <code>Request.GetCurrentRequest()</code> to get a reference to the currently executing <code>Request</code> - but since this is a Java app I'm looking for something simliar that could be used to gain access to <code>ServletConfig</code>.</p>

[{'answer_id': 41767, 'author': 'Tim Howland', 'author_id': 4276, 'author_profile': 'https://Stackoverflow.com/users/4276', 'pm_score': 1, 'selected': False, 'text': "<p>Have you considered using the Spring framework for this? That way, your beans don't get any extra cruft, and spring handles the configuration setup for you.</p>\n"}, {'answer_id': 41777, 'author': 'stjohnroe', 'author_id': 2985, 'author_profile': 'https://Stackoverflow.com/users/2985', 'pm_score': 0, 'selected': False, 'text': '<p>I think that you will have to add an associated bootstrap class which takes a reference to a ServletConfig (or ServletContext) and transcribes those values to the Factory class. At least this way you can package it separately.</p>\n\n<p>@toolkit : Excellent, most humbled - This is something that I have been trying to do for a while</p>\n'}, {'answer_id': 41814, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 4, 'selected': True, 'text': '<p>One way you could do this is:</p>\n\n<pre><code>public class FactoryInitialisingServletContextListener implements ServletContextListener {\n\n    public void contextDestroyed(ServletContextEvent event) {\n    }\n\n    public void contextInitialized(ServletContextEvent event) {\n        Properties properties = new Properties();\n        ServletContext servletContext = event.getServletContext();\n        Enumeration&lt;?&gt; keys = servletContext.getInitParameterNames();\n        while (keys.hasMoreElements()) {\n            String key = (String) keys.nextElement();\n            String value = servletContext.getInitParameter(key);\n            properties.setProperty(key, value);\n        }\n        Factory.setServletContextProperties(properties);\n    }\n}\n\npublic class Factory {\n\n    static Properties _servletContextProperties = new Properties();\n\n    public static void setServletContextProperties(Properties servletContextProperties) {\n        _servletContextProperties = servletContextProperties;\n    }\n}\n</code></pre>\n\n<p>And then have the following in your web.xml</p>\n\n<pre><code>&lt;listener&gt;\n    &lt;listener-class&gt;com.acme.FactoryInitialisingServletContextListener&lt;listener-class&gt;\n&lt;/listener&gt;\n</code></pre>\n\n<p>If your application is running in a web container, then the listener will be invoked by the container once the context has been created. In which case, the _servletContextProperties will be replaced with any context-params specified in the web.xml.</p>\n\n<p>If your application is running outside a web container, then _servletContextProperties will be empty.</p>\n'}]
['https://Stackoverflow.com/questions/41659', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4249/']