tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Prashant sharma" <prash...@filmtvindia.com>
Subject Dynamically ready properties file or context path
Date Wed, 17 Apr 2002 11:40:17 GMT
Hi friends ... 

I want help regarding reading xyz.properties file from a servlet. 

I know i can use something like
getServletContext().getRealPath("."); 


But unfortunately when I try to use this in the following example i get 
error,
as there i'm using static method and getServletContext is non static. 

And I want to stick to static method in this class  .. (correct me if i'm 
wrong). 

Now i just want to know how can i find another solution to it.
I want to dynamically read properties file (which has DB connection 
details). 

How shall i do it??? 

or how can i find $CATALINA_HOME in servlet.? 

Help is appreciated ... 

Thanking u,
Ranjeet 

If u've patience to read then 


Following is the code which i'm using... 

///DB.java  file start 

package com.fc.beans;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*; 

public class DB extends HttpServlet{ 

	public static String propertyFile= 
"/temp5/sites/abc/www/properties/jdbc.properties";
/// Above i've given the absolute path :-( 

	private DB(){
	} 

	public static Connection connect() throws SQLException{
		Error er=null;
		try{
			InputStream in=new FileInputStream(propertyFile);
			Properties p= new Properties();
			p.load(in);
			in.close();
			String driver=p.getProperty("jdbc.driver.class");
			String url=p.getProperty("jdbc.driver.url");
			String uid = p.getProperty("username");
			String pwd = p.getProperty("password");
			Class.forName(driver);
			return DriverManager.getConnection(url,uid,pwd);
		}
		catch(IOException e){
			er.log("Cannot read property file"+e.getMessage());
			throw new SQLException("Cannot read property file: "+e.getMessage());
		}
		catch(ClassNotFoundException e){
			er.log("Cannot load driver"+e.getMessage());
			throw new SQLException("Cannot load driver: "+e.getMessage());
		}
	} 

	public void main(String args[]) throws SQLException,Exception{
		connect().close();
		System.out.println("Connected Successfully!");
	} 

}
////////////file end

--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message