假设你的war包名字叫做 MyApp.war,你要读取的文件是 /WEB-INF/config/myConfig.xml,你可以这样读:
public class ContextListener implements ServletContextListener {
    
    @Override
    public void contextDestroyed(ServletContextEvent sce)
    {
        // TODO Auto-generated method stub
    }    @Override
    public void contextInitialized(ServletContextEvent sce)
    {   
        String path = sce.getServletContext().getRealPath("/");
        File file = new File(path + "/WEB-INF/config/myConfig.xml");
    }}
其实你完全可以不需要写ServletContextListener的,只要你能够获取到ServletContext对象就可以了。

解决方案 »

  1.   

    属性文件可以这样:public class MyConfig { /**
     * 属性文件
     */
    private static GBKProperties prop = new GBKProperties(); /**
     * 属性文件路径
     */
    private static final String CONFIG_FILE = "/myConfig.properties"; /**
     * 应用路径
     */
    private static String configPath; /**
     * 当前类实例
     */
    private static MyConfig instance = new MyConfig(); /**
     * 获取实例
     * @return
     */
    public static MyConfig getInstance() {
    return instance;
    } /**
     * 构造函数
     */
    private MyConfig() {
    try {
    URL url = (com.mm.myConfig.class)
    .getResource(CONFIG_FILE);
    String s = url.toString();
    if (s.startsWith("file:/")) {
    configPath = s.substring(5, s.length()
    - "/WEB-INF/classes/myConfig.properties".length());
    }
    InputStream in = url.openStream();
    prop.load(in);
    in.close();
    } catch (Exception e) {
    throw new SomeException("NoConfig", e);
    }
    }
    }