We have a small Provisioning server which only hosts servlets. Hosted on tomcat. There are few values hardcoded in the servlet which i want to make configurable or external, so that they can be modified without changing the servlets. Can anyone please suggest what are my Options?
4 Answers
Options I can think of :
- Define them as servlet init parameters in DD(web.xml) or using annotation , if they are specific to Servlet. Look at this Oracle tutorial
- Define them as context parameters in DD(web.xml) or using annotation , if they are common for the entire web app.
- Define them in an external properties file . You can then load the properties file kept in the classpath.
Java EE 7 tutorial - Servlets (Servlet 3.1)
P.S: I have just given you pointers , you can get the examples of how to achieve that , easily in internet.
 
    
    - 48,828
- 16
- 130
- 164
- 
                    in case the OP is wondering, DD referred here is the Deployment Descriptor which is the web.xml. – asgs Jul 18 '13 at 06:11
There are a few options:
- If the values are servlet specific, you can configure them as Servlet Init-Parameter, in the 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 get them using - ServletConfig#getInitParameter(String):- getServletConfig().getInitParameter(paramName);
- If the values are web-app specific, you can configure them as Context parameter: - <web-app ...> <context-param> <param-name>${param-name}</param-name> <param-value>${param-value}</param-value> </context-param> </web-app>- And get them using - ServletContext#getInitParameter(String):- getServletContext().getInitParameter(paramName);
- Another option is to have those values in a properties file, and load values from it in the servlet. You can add the properties file to the Web-App classpath (you can put it inside the - /WEB-INF/classesfolder, or if you are using Eclipse IDE, just put it inside the- /srcfolder, and load it as resource:- Properties props = new Properties(); props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("webapp.properties"));
See Also:
 
    
    - 1
- 1
 
    
    - 209,639
- 45
- 409
- 525
You can provide init params to a servlet, which can be configured in your web.xml. This tutorial should help you achieve what you need:
 
    
    - 67,789
- 12
- 98
- 136
As all said there are many ways.This is Another approach(This is what I am doing right now)
A Constants Class (Public Static String constants)
A XMl  file called properties.xml  for example veriosn name,branch name etc 
<property name="version">XX..XX</property>
<property name="branch">XX.13.</property> 
in web.xml
    <servlet>
    <servlet-name>StartUpServlet</servlet-name>
    <display-name>StartUpServlet</display-name>
    <servlet-class>com.nextenders.server.StartUpServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
    </servlet>
That servlet executes when you start your tomcat
And my StartUpServlet
public class StartUpServlet extends  HttpServlet{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Override
    public void init() throws ServletException {
        super.init();
        setVersion();  //I'l parse that file and assign constants.And use else where
    } 
So with out touching the App,change properties in xml and restart the App.
 
    
    - 120,458
- 37
- 198
- 307
