1

New to Java and I'm trying to find the equivalent for web.config application settings in .NET.

So far I have found examples of something like this:

Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/appSettings.properties");
props.load(fis);

However, I know that .NET caches the web.config file and inserting a call to Configuration.AppSettings[x] is not expensive. I'm looking for an equivalent solution. So that I can call for Application settings multiple times through out a page load.

For Example:

<link  href="<%= ResolveUrl("~/css/main.css?ver=" + Configuration.AppSettings["version"]) %>" rel="Stylesheet" type="text/css" media="screen" />

I can create a class that can do the equivalent, but i'd hate to reinvent the wheel if there is already something there.

9
  • 1
    Create a properties file and load it into your application through ServletContext. Commented Dec 18, 2013 at 15:08
  • Word of advice: it doesn't help you to try and make Java behave like .NET; the two platforms have completely different design philosophies. Rather try and find a way to do what you functionally want to achieve within the context of the Java (Enterprise?) platform and forget that .NET exists.
    – Gimby
    Commented Dec 18, 2013 at 15:12
  • 1
    The Java equivalent for .NET's Web.config is web.xml, the deployment descriptor for webapps, which is in the WEB-INF directory of your webapp. You can set parameters there which are accessible via the ServletContext as Luiggi mentioned.
    – Jesper
    Commented Dec 18, 2013 at 15:13
  • @Jesper: Thanks, could you point me to any examples or tutorials? And does it get cached?
    – capdragon
    Commented Dec 18, 2013 at 15:14
  • See for example: docs.oracle.com/javaee/6/tutorial/doc/bnafo.html
    – Jesper
    Commented Dec 18, 2013 at 15:15

3 Answers 3

2

The Java equivalent for .NET's Web.config is web.xml, the deployment descriptor for Java EE webapps.

In web.xml you can set context parameters, that you can access via the class ServletContext in your servlets or JSPs. For example:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.mycompany.myapp.SomeServlet</servlet-class>
</servlet>
<context-param>
    <param-name>myparam</param-name>
    <param-value>hello</param-value>
</context-param>

Inside your JSP:

// 'application' is an implicit variable available inside JSPs
// that refers to the ServletContext
String value = application.getInitParameter("myparam");
0

You can also use init-param in web.xml. I got this from Google Cloud Endpoint's example at https://cloud.google.com/appengine/docs/java/endpoints/required_files

<servlet>
    <servlet-name>SystemServiceServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.helloendpoints.Greetings</param-value>
    </init-param>
</servlet>

Another example from a post at Getting the init parameters in a servlet shows: Accessing servlet init parameter in a servlet for which it was defined in DD:

getServletConfig().getInitParameter("name");
0

https://commons.apache.org/proper/commons-configuration/ apache commons configuration is the best choice.

Automatic Reloading A common issue with properties file is to handle the reloading of the file when it changes. The sample code is found beneath:

PropertiesConfiguration config = new 
PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
1
  • Please explain why the given URL is the best choice.
    – this.josh
    Commented Jun 6, 2017 at 7:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.