Monojeet Nayak
Monojeet Nayak

Reputation: 721

Configurable Values in a servlet

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?

Upvotes: 1

Views: 781

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213203

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:

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49362

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.

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

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

Upvotes: 0

Related Questions