Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course you can't store hardcoded credentials in your web app.</p> <p>So you must store them outside, on the server on which you deploy.</p> <p>Outside of specific container features like datasources, you may :</p> <p>1) use java Preferences, but there are so many bugs with them, especially if using tomcat on linux, that I wouldn't consider them (I did, I cleaned)</p> <p>2) Put the connection properties in external files. Here's how I do it :</p> <p>I have in my WEB-INF directory a text file listing all authorized locations for those proprerties. This file is deployed as is on all servers. Here's an example :</p> <pre><code># This file lists all the locations where mysoft will look for # an XML config file when starting. # It's recommended to use links to simplify parameterization but # it's also possible to enrich this file. # standard windows locations c:\mycomp\mysoft\config\config.xml d:\mycomp\mysoft\config\config.xml # standard linux location /var/lib/mycomp/mysoft/config/config.xml </code></pre> <p>And my webapp simply chooses the first found file when initializing. To do this I implemented a ServletContextListener in which I read the text file and store the config.</p> <p>Here's a very simplified version of my listener :</p> <pre><code>public class MySoftServletContextListener implements ServletContextListener { public final static String CONFIG_LIST_PATH = "/WEB-INF/config-paths.txt"; @Override public void contextInitialized(ServletContextEvent contextEvent) { InputStream list = context.getResourceAsStream(WORLD_LIST_PATH); try { BufferedReader listReader = new BufferedReader(new InputStreamReader(list)); String line; while ((line=listReader.readLine())!=null) { if (line.length()&lt;4 || line.charAt(0)=='#') continue; File file = new File(line); if (file.exists()) { // log config from the file and, for example, store it in a singleton break; } else { log.info("No config in file " + line + " (file doesn't exist)"); } } list.close(); } catch (IOException e) { e.printStackTrace(); log.severe("Error while looking for a config : " + e.toString()); } } } </code></pre> <p>The servlet context listener is referenced in my web.xml like this :</p> <pre><code>&lt;listener&gt; &lt;listener-class&gt;com.mycomp.mysoft.webapp.MySoftServletContextListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload