Modify WebInitParam in Servlet 3.0 when the project in packaged in a WAR
java, servlet-3.0, servlets
Solution
AFAIK there is no standard way to modify the init params in runtime. Moreover, it's a bad practice to put configuration there, especially to put there database credentials in a clear text.
Usually the best practices is to put a configuration in an external file. It may be some custom properties or xml file.
For database connection it's common to use JNDI. So basically in the code you look for a JNDI resource, while the JNDI itself in configured at container level. Google to find a lot of examples how to configure database connection via JNDI for Jetty, Tomcat, JBoss and more.
Problem
In my project I'm using Servlet 3.0 and I've tried to use annotations. To init the connection parameters for the DB I use this in my servlet : ``` @WebServlet(name = "FrontServlet", urlPatterns = {"/index"}, initParams = { @WebInitParam(name = "userDB", value = "root"), @WebInitParam(name = "passwordDB", value = "*****")}) ``` Now when I packaged the project in a WAR I've no web.xml so I can't edit the init parameters as I used to do whith older servlet version. My question is, can I edit my init parameters when the project is packaged in a WAR? If yes how? Otherwise what approach should I use to store my init parameters and be able to modify them in the WAR? If possible I would like to avoid to recreate the whole web.xml with all URL-patterns, etc... EDIT : Finally I kept : ``` @WebServlet(name = "FrontServlet", urlPatterns = {"/index"}) ``` And I load DB params using Properties, accessing the config file with `getClass().getClassLoader().getResourceAsStream("servlet/config/config.ini")`