I am writing a Java servlet that requires reading some site-specific configuration data; I want to be easily accessible / modified by Sesadmin at the time of deploying it. There is no sensible default, so the site administrator has to provide .
There are some string keys / values added (looks like). It will be read only once (in the initial time)
I know and ServletContext.getInitParameter ()
mechanism, but as far as I understand, they require servlet package (either as a property file Or specified in web.xml
), which makes it uncomfortable for upgrading the servlet code.
Any "standard" interface for a servicelet can get this type of key / value configuration data? It can be fine that the programming interface is the same everywhere, but the actual way to set up configuration data depends on the actual servlet container.
I'm looking forward to portable solutions, there should be content with something that only works in tokkat and jetties.
The recommended way of configuring an application server is a JDDI per web application.
Each application server (including JetType and TOMAX) allows you to configure JNTI parameters.
To add your JTT XML JNDI parameter param.file
to JT:
& Lt; Arg Type = "java.lang.String" & gt; & Lt; System Property Name = "Jetty Home" default = "." /> Etc / config.properties & lt; / Arg> & Lt; Arg type = "boolean" & gt; True & lt; / Arg> & Lt; / New & gt;
Then you can read the JNDI parameter in your servlet:
import javax.naming.InitialContext; Import javax.naming.NamingException; ... Read Public Objects Jndy (String Paramount) {Object jndiValue = null; {Try the final InitialContext ic = new InitialContext (); JndiValue = ic.lookup ("java: comp / env /" + nickname); } Hold (naming exception e) {// handle exception} return jndiValue; } Public string getConfigPath () {return (string) read jedi ("param.file"); }
The way to set the JNDI value is different for other application servers but the code is always the same to read the configuration.
Comments
Post a Comment