1

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?

Monojeet Nayak
  • 661
  • 1
  • 7
  • 13

4 Answers4

1

Options I can think of :

  1. Define them as servlet init parameters in DD(web.xml) or using annotation , if they are specific to Servlet. Look at this Oracle tutorial
  2. Define them as context parameters in DD(web.xml) or using annotation , if they are common for the entire web app.
  3. 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.

NINCOMPOOP
  • 46,742
  • 15
  • 123
  • 158
  • 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
1

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/classes folder, or if you are using Eclipse IDE, just put it inside the /src folder, and load it as resource:

    Properties props = new Properties();
    props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("webapp.properties"));
    

See Also:

Community
  • 1
  • 1
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
0

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:

http://www.javatpoint.com/servletconfig

Juned Ahsan
  • 63,914
  • 9
  • 87
  • 123
0

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.

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284