File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how to use relative path in files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to use relative path in files?" Watch "how to use relative path in files?" New topic
Author

how to use relative path in files?

narendra cheeterala
Greenhorn

Joined: Oct 09, 2012
Posts: 4

i am designing a web project in MVC pattern, so i created a properties file & used that file to load properties for database.
my code is:

public static boolean loadDBProperties(){
try{
p = new Properties();

fis = new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/LoginApplicationMVC1/WEB-INF/properties/pro.properties");

p.load(fis);

return true;

}
catch(Exception e){

return false;
}

}

//creating and returning connection object
public static Connection getConnection()
{

try{
if(loadDBProperties()){

Class.forName(p.getProperty("db.driver"));

con = DriverManager.getConnection(p.getProperty("db.url"),p.getProperty("db.userid"),p.getProperty("db.password"));

return con;
}

return null;

}
catch(Exception e){

return null;
}
}


In that code how to use relative path to load properties file........
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Place the file under the WEB-INF folder and use the ServletContext.getResourceAsStream() method to open a stream to the file.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pablo Abbate
Ranch Hand

Joined: Aug 06, 2012
Posts: 30

Are you using spring framework, if not please use Resource Bundle and put the properties file into web-inf classes folder.


Nissi Group, Posicionamiento Web, Desarrollo de Software, DiseƱo Web
Blog , LinkedIn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use relative path in files?
 
Similar Threads
Here's a fun one...
Connection Pooling with Commons DBCP - spot the problem?
below code work in command line. but it doesn't work in eclipse.
In struts I am getting an error
Servlet Cannot find Find Properties File