日期:2014-05-17  浏览次数:21040 次

java里面怎么读取web.xml里面的context-param值
用MyEclipse创建了一个web项目,在web.xml的context-param节点定义数据库的连接信息以及账号密码等,应该怎么读取context-param的值,要引入哪些包,使用getServletContext().getInitParameter,总是提示没有定义方法
------解决方案--------------------
Servlet必须要继承HttpServlet抽象类,而HttpServlet继承了GenericServlet抽象类,
getServletContext()方法是GenericServlet抽象类内部定义的方法,所有才会不需要对象就可直接调用该方法获取ServletContext对象
------解决方案--------------------
一般用户配置都封装在properties文件中,
假如楼主真要存放在web.xml中,那么也可以读取,但是一定要在servlet中

 public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletExceptionIOException {  
       String 参数值 = this.getServletContext().getInitParameter("参数名");  
  }