Read Parameters from web.xml in Servlet -- release the work of programmer

If you have developed one Servlet application that uses MySQL database (or others), and you are using the local database for the debuging and programming, normally, I have used to define the database server host, user name,password as the pulbic static variant in some class, and changed them when I upload the compiled class to the server or the customers, because the server database or the customers' will not use the server host, user name or password same as yoursFoot in mouth.  After some time of debuging, uploading new classes, I am tired of changing those parameters every time, and I found one problem -- could let the users change such parameter themself, not me? That means the parameters will not be compiled in the class, but written in one separated file, and the users have the right to edit the file in the server.

Java has considered every thing for me, we could set those parameters in web.xml and read them in servlet.

You could add such nodes in the <web-app> node in web.xml, I hope you know where it is.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>www.easywms.
testServlet</servlet-class>
  </servlet>

........

<context-param>
    <description>This is mysql database host, ie. localhost</description>
    <param-name>database.host</param-name>
    <param-value>localhost</param-value>
</context-param>
 <context-param>
    <description>This is mysql database host port, ie. 3308 for demo, 3306 for dev</description>
    <param-name>database.port</param-name>
    <param-value>3308</param-value>
</context-param>
<context-param>
     <description>This is mysql database name</description>
    <param-name>database.name</param-name>
    <param-value>yourdatabasename</param-value>
</context-param>
 <context-param>
     <description>This is mysql database user name, root for demo, gofer for dev</description>
    <param-name>database.user</param-name>
    <param-value>yourusername</param-value>
</context-param>
<context-param>
     <description>This is mysql database password</description>
    <param-name>database.password</param-name>
    <param-value>yourpassword</param-value>
</context-param>  
.............................

 </web-app>

ok, now open you servlet source,  in the method, doPost, doGet or init (I prefer in init method), you can use such code to read the parameters:

String databaseHost = getServletContext().getInitParameter("database.host");
String databasePort = getServletContext().getInitParameter("database.port");
String databaseName = getServletContext().getInitParameter("database.name");
String databaseUser = getServletContext().getInitParameter("database.user");
String databasePassword = getServletContext().getInitParameter("database.password");  

 Is that easy? then the customer or user could change the parameters as theirs own,without asking you to change them. I could have a rest.

Here is the advantages of specifing parameter values in web.xml:

  • Using your own settings file requires additional coding and management.
  • Hard-coding parameter values directly into your application
    code makes them more difficult to change in the future, and more
    difficult to use different settings for different deployments (eg: JDBC
    settings, mail server address).
  • Other developers using your code will be able to find any
    relevant parameters more easily, as this is a standard location for
    such parameters to be set. 


Not finished, if you have only one servlet or filter, you could try this way:

String value = getServletConfig().getInitParameter("parameterName");

And add such node in one <servlet> node:

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.easywms.TestServlet</servlet-class>
    <init-param> 
<description>test page</description> 
<param-name>databaseHost</param-name> 
<param-value>localhost</param-value> 
</init-param> 
</servlet>

Only when you call this servlet (TestServlet in this case), you can read the parameters; for the other servlet, it does not work.

 Ref:
http://wiki.metawerx.net/wiki/AdvantagesOfSpecifyingParameterValuesInWeb...
http://wiki.metawerx.net/wiki/Web.xml.InitParam
http://wiki.metawerx.net/wiki/Web.xml.ContextParam


Blog