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"
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>


    <description>This is mysql database host, ie. localhost</description>
    <description>This is mysql database host port, ie. 3308 for demo, 3306 for dev</description>
     <description>This is mysql database name</description>
     <description>This is mysql database user name, root for demo, gofer for dev</description>
     <description>This is mysql database password</description>


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("");
String databasePort = getServletContext().getInitParameter("database.port");
String databaseName = getServletContext().getInitParameter("");
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:

<description>test page</description> 

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