简体   繁体   中英

file properties not reconised in dynamic web project

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")); 
    
}

弧形目录:

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);        
}

If you put this file in the root directory of the webapplication, it might by published to the inte.net. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM