首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

java web ServletContext.getRealPath(String str)步骤的使用

2012-10-06 
java web ServletContext.getRealPath(String str)方法的使用由于数据库的配置我想做成配置在一个XML文件

java web ServletContext.getRealPath(String str)方法的使用

由于数据库的配置我想做成配置在一个XML文件里,不过,怎么读取都不成功,,后来我在网上查找,通过下面的方式在servlet获取文件的绝对路径,不过由于多了一个/,还是错误

? ServletContext context = getServletContext();
????? String? path = context.getRealPath("/") + "/WEB-INF/drivers.xml";

为了这个我还在另外一个servlet测试了一下,才发现了,呵呵,改成

?String? path = context.getRealPath("/") + "WEB-INF/drivers.xml";

哈哈一切都搞定了

import javax.servlet.http.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.*;

?public void init(ServletConfig config) throws ServletException {
?????? super.init(config);
?????? ServletContext context = getServletContext();
????? String? path = context.getRealPath("/") + "WEB-INF/drivers.xml";
?????? DriverUtilities2.loadDrivers(path);
?????? ConnectionSource.init(getInitParameter("vendor"),
???????????????????????????? getInitParameter("host"),
???????????????????????????? getInitParameter("dbName"),
???????????????????????????? getInitParameter("username"),
???????????????????????????? getInitParameter("password"));
?
??
?}

热点排行