Ali Raza
Ali Raza

Reputation: 1215

where do i need to put a file in web appliction so that I can read it from servlet

I need to know where should I put a file in web application so that I could read it from servlet.

up until now, I have placed a folder named conf/config.cfg in the following structure

BankConfig/
              WEB-INF/
                       libs/
                       classes/
              conf/
                    config.cfg

and I'm reading it from a servlet like the following:

    String fileName = "conf/config.cfg";
    InputStream is;
    try {
        is =// getClass().getResourceAsStream(fileName);
        new FileInputStream(fileName);
        prop.load(is);
                }catch(Exception e){}

Upvotes: 0

Views: 123

Answers (1)

Buhake Sindi
Buhake Sindi

Reputation: 89169

If you want to read it from the servlet, I would use ServletContext to read the file and properties.

Example:

String fileName = "/conf/config.cfg";
InputStream is = getServletContext().getResourceAsStream(fileName);
prop.load(is);

Note: The fileName starts with a / (root from servlet context perspective). The only thing is I would have put the conf/config.cfg inside the WEB-INF folder (as it's hidden from view by the Servlet container, and do the same as above, instead I'll change the file name as:

String fileName = "/WEB-INF/conf/config.cfg";

Upvotes: 2

Related Questions