java - Configurable Values in a servlet -


we have small provisioning server hosts servlets. hosted on tomcat. there few values hardcoded in servlet want make configurable or external, can modified without changing servlets. can please suggest options?

there few options:

  • if values servlet specific, can configure them servlet init-parameter, in deployment descriptor (the web.xml file):

    <servlet>         <servlet-name></servlet-name>     <servlet-class></servlet-class>     <init-param>         <param-name>${param-name}</param-name>         <param-value>${param-value}</param-value>     </init-param> </servlet> 

    and them using servletconfig#getinitparameter(string):

    getservletconfig().getinitparameter(paramname); 
  • if values web-app specific, can configure them context parameter:

    <web-app ...>     <context-param>         <param-name>${param-name}</param-name>         <param-value>${param-value}</param-value>     </context-param> </web-app> 

    and them using servletcontext#getinitparameter(string):

    getservletcontext().getinitparameter(paramname); 
  • another option have values in properties file, , load values in servlet. can add properties file web-app classpath (you can put inside /web-inf/classes folder, or if using eclipse ide, put inside /src folder, , load resource:

    properties props = new properties(); props.load(thread.currentthread().getcontextclassloader().getresourceasstream("webapp.properties")); 

see also:


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -