上下文 ServletContext

ServletContext
    1, 描述:
        一个web 应用只有一个ServletContext对象,
        所有的servlet 都共享这个ServletContext 对象,
        又称为Application,
        也是一个作用域:
            1) 空间: 最广, 被所有的servlet 共享
            2) 时间: 最长, 从web应用被加载到被卸载都有效
    2, 常见用法:
        1) 读取web.xml 中<context-param> 参数
        2) 作用域:  setAttribute    //设置属性
                    getAttribute    //返回属性
                    removeAttribute    //删除属性
        3) 获取文件的绝对路径: getRealPath("index.html");
        4) 用于获取目录下的所有目录和文件的路径, 返回Set 类型
            getResoursePaths("/picture");
        5) 用于获得web 应用中的资源: getResourceAsStream("/1.properties")
    3, 获得对象:
        1) ServletContext context = this.getServletContext();
        2) 在其他文件中使用ServletContext时需要
            this.getServletContext().getAttribute("c");

猜你喜欢

转载自blog.csdn.net/sidiansh/article/details/39968651