---
title: "JSP-->web应用中属性文件使用 (转载)   "
author: "ericjoe"
date: "2007-09-27T21:41:00Z"
tags: []
slug: "jsp-web"
draft: false
lastmod: "2007-09-27T21:41:00Z"
id: 393851
url: "https://coffeef.iteye.com/blog/393851"
---

在web应用中，一些数据库连接参数或者系统本身的参数通常不是写在程序中的，需要保存成属性文件的形式或者XML文件的形式。二者各有优缺点，属性文件的形式操作和管理比较简单，XML文件形式则能提供很强大，并且层次性很好的属性文件的配置。  
        下面讲解在web应用中通过属性文件的方式来记录一些重要的参数。  
        下面的例子以一个数据库连接参数为例子，开发环境是Eclipse，部署环境是Tomcat。  
        属性文件内容如下所示：  
 init.properties  
 -------------------------------------------------------------  
 drivers=oracle.jdbc.driver.OracleDriver  
 url=jdbc:oracle:thin:@127.0.0.1:1521:test  
 username=testuser  
 password=test  
 -------------------------------------------------------------  
   
 访问该属性文件的代码：  
InitPropertiesLoader.java  
\-------------------------------------------------------------------  
package com.knight.commons  
public class InitPropertiesLoader  
{  
 /\**  
     * 直接读取classpath中的属性文件  
     * @param filename 文件名称和路径  
     * @return  
     \*/  
    public Properties getProperties(String filename)  
    {  
        Properties prop = new Properties();  
        InputStream in = null;  
        try  
        {  
            in = getClass().getResourceAsStream(filename);  
            prop.load(in);  
        }  
        catch (Exception e)  
        {  
            log.info("无法正确读取数据库连接配置属性文件！");  
        }  
        finally  
        {  
            try  
            {  
                if (in != null)  
                {  
                    in.close();  
                }  
            }  
            catch (Exception e)  
            {  
            }  
        }  
        return prop;  
    }  
}  
\-------------------------------------------------------------------

ConnectionManager.java  
\-------------------------------------------------------------------  
package com.knight.commons.database  
public class ConnectionManager  
{  
 private static String driverName = null;  
    private static String url = null;    
    private static String username = null;  
    private static String password = null;  
    private static Properties prop = null;  
     
    public void init()  
    {  
     try  
        {  
            prop = new InitPropertiesLoader().getProperties("/init.properties");  
            driverName = prop.getProperty("drivers");  
            url = prop.getProperty("url");  
            username = prop.getProperty("username");  
            password = prop.getProperty("password");  
        }  
        catch (Exception e)  
        {  
            log.info("读取属性配置文件时出错！");             
        }  
        finally  
        {  
            try  
            {  
                if (prop != null)  
                    prop.clear();  
            }  
            catch (Exception e)  
            {  
                log.info(e.getMessage());  
            }

        }  
    }  
     
    public static Connection getConnection()  
    {  
        Connection conn = null;  
        //初始化数据库连接参数  
        init();  
         
        try  
        {  
            Class.forName(driverName);  
            conn = DriverManager.getConnection(url, username, password);  
        }  
        catch (Exception e)  
        {  
            log.info("There is some error when you get a connection.");  
            log.info(e.getMessage());  
        }  
        return conn;  
    }  
}  
\-------------------------------------------------------------------  
        上面就是涉及到的几个主要文件，为了既能在IDE环境中使用该属性配置文件，又能在实际的web应用中保证该属性文件的正常调用。该属性文件存放的位置很重要。  
        "/init.properties"表示该属性文件存放在CLASSPATH的跟目录中，在Eclipse环境下存放在工程/bin下。  
         在web应用中init.properties存放在当前应用的WEB-INF/classes目录下。更简单的处理方式是将init.properties打到工程的包中。这样，当多个应用部署在同一个Context下时，相互之间能够保持独立性。但是这时候千万要注意同一个Context下多个应用的属性文件是否文件命名冲突。  
 附打包后目录的层次：  
            /  
                       init.properties  
           /com/knight/commons  
                       InitPropertiesLoader.class  
          /com/knight/commons/database  
                      ConnectionManager.class