Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to portably read configuration data from a servlet

I'm writing a Java servlet that needs to read some site-specific configuration data; I would like it to be easily accessible/modifiable by the sysadmins at deployment time. There is no sensible default, so the data has to be provided by the site admin.
It consists of a few string key/value pairs (think Properties). It would only be read once (at initialization time).

I'm aware of this SO question and the ServletContext.getInitParameter() mechanism, but as far as my understanding goes, they require the data to be bundled in the servlet package (either as a properties file, or specified in the web.xml), which makes it inconvenient to upgrade the servlet code.

Is there any "standard" interface for a servlet to get this kind of key/value configuration data? It would be ok if the programming interface is the same everywhere, but the actual way of setting the configuration data depends on the actual servlet container being used.

I'm looking preferably at portable solutions, but I'd be content with something that only works in Tomcat and Jetty.

like image 838
Riccardo Murri Avatar asked Sep 27 '10 13:09

Riccardo Murri


People also ask

Which object stores configuration information specific to a servlet?

ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization. ServletConfig is for a particular servlet, which means one should store servlet-specific information in web.

What does servlet config interface do?

Interface ServletConfig A servlet configuration object used by a servlet container to pass information to a servlet during initialization.

What is the correct syntax for getting the form data inside a servlet code?

We need to use either doGet() or doPost() method in the servlet class to get the information from the browser based on the method specified in the form.


1 Answers

The recommended way to configure an application server for a web application is per JNDI.

Every application server (including Jetty and Tomcat) allows you to configure JNDI parameters.

For Jetty you can add the following to your jetty.xml to add the JNDI parameter param.file:

<!--  JNDI java:comp/env --> 
<New id="param.file" class="org.mortbay.jetty.plus.naming.EnvEntry">
  <Arg>param.file</Arg> 
  <Arg type="java.lang.String"><SystemProperty name="jetty.home" default="."/>etc/config.properties</Arg> 
  <Arg type="boolean">true</Arg> 
</New> 

Then in your servlet you can read the JNDI parameter:

import javax.naming.InitialContext;
import javax.naming.NamingException;

...

public Object readJndi(String paramName) {
  Object jndiValue = null;
  try {
    final InitialContext ic = new InitialContext();
    jndiValue = ic.lookup("java:comp/env/" + paramName);
  } catch (NamingException e) {
    // handle exception
  }
  return jndiValue;
}


public String getConfigPath() {
  return (String) readJndi("param.file");
}

The way to set JNDI values differs for other application servers but the code to read the configuration is always the same.

like image 101
vanje Avatar answered Oct 11 '22 18:10

vanje