在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
分享到:
相关推荐
WEB.XML标准配置注解: <!--Servlet的映射,用来说明客户端IE中输入什么样的地址字符串对应...--定义了Web应用程序中会话的超时时限--> <session-config> <session-timeout>10</session-timeout> </session-config>
精通JSP-Web开发技术与典型应用的范例代码。
04 JAVA SE 环境搭建 --> 基础程序 --> 面向对象 --> 应用开发 --> 高级应用 --> Java新特性 --> JDBC 05 JAVA EE WEB HTML --> JavaScript --> JSP --> JavaBean --> DAO --> Smartupload --> Servlet --> MVC 06 ...
张新曼的精通jsp-web开发技术与典型应用 代码齐全 适合学生学习使用
JSP-Web 开发技术与典型应用.rar JSP-Web 开发技术与典型应用.rar JSP-Web 开发技术与典型应用.rar
4.将以下代码添加到web.xml配置文件中: <servlet> <servlet-name>Connector</servlet-name> <servlet-class> com.FCKeditor.connector.ConnectorServlet </servlet-class> <init-param> <param-name>baseDir...
-- 此处说明定义的java类文件"*.class",系统默认是从%CATALINA_HOME%\WEB-INF\classes\找类文件 值得注意的是其中"test."表示两种意思:1)类文件既HelloWorld.class在classes文件夹下的test文件夹内; 2)HelloWorld...
NULL 博文链接:https://lingf.iteye.com/blog/1108467
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/database.xml /WEB-INF/applicationContext.xml </param-value> </context-param> ... </jsp-config>
精通JSP-Web开发技术与典型应用随书光盘
精通JSP-Web开发技术与典型应用
精通JSP-Web开发技术与典型应用
精通JSP-Web开发技术与典型应用的(附件光盘) 教程的附赠光盘,里面都是案例
精通JSP-Web开发技术与典型应用
-- local web url --> <init-param> <param-name> edu.yale.its.tp.cas.client.filter.serverName </param-name> <param-value>www.teste.com:8080</param-value> </init-param> </filter> <!-- ...
5、把WEB-INF文件夹下的web.xml文件中的一下部分粘贴到网站根目录的WEB-INF的web.xml中; <display-name>defaultroot</display-name> <servlet> <servlet-name>debugjsp</servlet-name> <description>Added to ...
Window->preference->MyEclips->Files and Editors->JSP->JSP Source->选择 Indent using spaces,Indentation size 选择4 Window->preference->MyEclips->Files and Editors->SQL->SQL Source->选择 Indent using ...
请在web.xml文件中使用– page >元素为整个web应用程序设置错误处理页面。已知:处理状态码为404的页面为404.jsp,处理状态码为500的页面为500.jsp。