现在的位置: 首页 > 综合 > 正文

ServletContext .getRealPath

2013年10月12日 ⁄ 综合 ⁄ 共 948字 ⁄ 字号 评论关闭

哈哈,终于搞定了,太高兴了

由于数据库的配置我想做成配置在一个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"));
 
  
 }

抱歉!评论已关闭.