0

I have a Dynamic web project ,in that i am creating config.properites file for Data base connectivity.

SampleProject 
  ->WebContent
    ->WEB_INF
       ->config.properties
       ->Index.jsp

i am reading Connectivity Details from configuration file in Index.jsp as follows

           Properties props = new Properties();        
          FileInputStream in = new FileInputStream("WEB-INF/config.properties");
            props.load(in);
            in.close();
            String driver = props.getProperty("driver");              
            String url = props.getProperty("url");
            String username = props.getProperty("username");
            String password = props.getProperty("password");                 
            class.forName(driver);
Connection con = DriverManager.getConnection(url, username, password);

but it's throwing

   org.apache.jasper.JasperException: An exception occurred processing JSP page /Index.jsp at line 66

    63:             
    64:            
    65:             FileInputStream in1 = 
    66:                     new FileInputStream("WEB-INF/config.properties");
    67:             props.load(in1);
    68:             in.close();
    69:        
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:447)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause    
java.io.FileNotFoundException: WEB-INF/config.properties (No such file or directory)
    java.io.FileInputStream.open(Native Method)
    java.io.FileInputStream.<init>(FileInputStream.java:137)
    java.io.FileInputStream.<init>(FileInputStream.java:96)

exception.. can anyone help me how to give the directory path?
4
  • you should try this:project->webcontent->web_inf->index.jsp Commented Apr 8, 2014 at 10:02
  • will you pls paste full stacktrace Commented Apr 8, 2014 at 10:07
  • wrong way to access the properties Commented Apr 8, 2014 at 10:17
  • WEB-INF is a secured folder and it can not be accessed like this... Commented Apr 8, 2014 at 10:20

2 Answers 2

2

It's not a very good idea to put configuration files in the web content directoy, which might make it accessible from the outside. I would rather put the config file somewhere in the classpath and use Thread.currentThread().getContextClassLoader().getResource('config.properties') to get an InputStream and load the property file.

Edit: Question has been edited. Having config under WEB-INF is better. Then you can use the methods of ServletContext to load the resource.

Sign up to request clarification or add additional context in comments.

Comments

0

If you really need to store your properties in WEB-INF, you can try to resolve the file system path where it is deployed to using ServletContext.getRealPath().

However, I'd also recommend moving the file to the classpath and load it as a resource, as NilsH suggests in his answer. Once you do that, you can also make use of ResourceBundle to read the properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.