0

I have a conf.properties in racine of webApplication and i have a method init in a class manageBDD to initialise parameters.

i call the method init in a servlet but the file is not reconised, when i call the method in the method main of class ManageBDD the method work.

Why in servlet the file is not reconised and in method main of class ManageBdd the file is reconised ?

(throws Exception is just for test the method...)

    public static void init() throws Exception {
    
    FileInputStream file = new FileInputStream("conf.properties");
    
    Properties prop = new Properties();
    prop.load(file);
    
    pDriver = prop.getProperty("jdbc.driver");
    pUrl = prop.getProperty("jdbc.url");
    pLogin = prop.getProperty("jdbc.login");
    pPass = prop.getProperty("jdbc.pass");
    
    System.out.println("in class : " + prop.getProperty("jdbc.login"));
    System.out.println("in class : " + prop.getProperty("jdbc.pass"));
    System.out.println("in class : " + prop.getProperty("jdbc.driver"));
    System.out.println("in class : " + prop.getProperty("jdbc.url")); 
    
}

arc directories :

in main ManageBDD, method work :

    public static void main(String[] args) throws Exception {
    
    init();
    
}

in servlet doesn't work when i run the server :

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    try {
        ManageBDD.init();
    } catch (Exception e) {
        e.printStackTrace();
    }

    //request.getRequestDispatcher("/index.jsp").forward(request, response);        
}
  • 1
    The properties file must be in your classpath. When run from Eclipse it's probably automatically added but running with your servlet container it is not. Not sure how you manage your project (not Maven?) but you need to see how to add the file to the packaged resources so that it's in the classpath. – Gaël J Jan 01 '23 at 11:42

1 Answers1

0

If you put this file in the root directory of the webapplication, it might by published to the internet. Why aren't you using the connection pool of your servlet container to manage your DB connections? For Tomcat it is explained here

And within your servlet you just can get it from the container. The only thing to look at, is to give the connection back to the pool by just closing it right after the things you've done with your DB.

If you do not want to do it, you can also use the web.xml file ( it is in the WEB-INF directory ) to store values used within your webapplication. And then you do not need to care about the location.

Fredy Fischer
  • 458
  • 3
  • 12
  • yes I did it in the web.xml file with 'context-param' and the 'init()' method in the servlet, I was testing several ways to do it. but I still block with the properties, – Nicolas Zanforlini Jan 02 '23 at 10:31
  • i need to search how to use the .properties – Nicolas Zanforlini Jan 02 '23 at 10:32
  • There might be different way to load it. One simple thing might be putting the fully qualified name of the file as a context-param in web.xml. Or,try to get it with InputStream is = this.getClass().getRessourceAsStream({fullyqualified name of the props file}). There is an example here https://stackoverflow.com/questions/27155195/how-getclassloader-getresourceasstream-works-in-java – Fredy Fischer Jan 03 '23 at 09:40