Servlet上下文 ServletContext

上下文接口    ServletContext接口
    每一个应用都有唯一的一个上下文对象,即为ServletContext对象
    ServletContext对象可以和请求、会话对象一样处理属性
        setAttribute
        getAttribute
        removeAttribute
    当容器启动时,会加载容器中的每一个应用,并针对每一个应用创建一个对象,称为上下文(context)对象。每一个应用只有一个唯一的上下文对象。Servlet API中提供了ServletContext接口来表示上下文对象。例如,Tomcat下有一个项目capter10,启动tomcat的时候,就会为capter10初始化唯一一个ServletContext独享,作为全局的信息存储空间。
    
    获取上下文对象:
        Servlet.getServletConfig(), ServletConfig.getServletContext()
        或者使用HttpServlet中的getServletContext()方法
    ServletContext接口中有很多方法,其中较为常见的是用来存储、获取上下文属性的方法,方法名和含义与请求和会话中的相关方法类似。
     Object getAttribute(String name)
          Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
    Enumeration getAttributeNames()
          Returns an Enumeration containing the attribute names available within this servlet context.
    ServletContext getContext(String uripath)
          Returns a ServletContext object that corresponds to a specified URL on the server.
    String getContextPath()
          Returns the context path of the web application.
    String getInitParameter(String name)
          Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
    Enumeration getInitParameterNames()
          Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.
    RequestDispatcher getNamedDispatcher(String name)
          Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
    String getRealPath(String path)
          Returns a String containing the real path for a given virtual path.
    RequestDispatcher getRequestDispatcher(String path)
          Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
    void removeAttribute(String name)
          Removes the attribute with the given name from the servlet context.
    void setAttribute(String name, Object object)
          Binds an object to a given attribute name in this servlet context.
          
    使用例子:
        使用ServletContext进行登录人次的统计。
        在jsp中,直接使用application内置对象就是该应用的ServletContext
        多次登录成功后,计数信息将被累加,但是,如果服务器重启,或者应用被重新加载,上下文对象将被小辉,那么计数器将被清0,解决这个问题就需要使用监听器。
        
    上下文参数
    在web.xml中可以配置上下文参数,在整个上下文中有效
    <context-param>
        <param-name>path</param-name>
        <param-value>/WEB-INF/props</param-value>
    </context-param>
    获取上下文参数的方法:
        public String getInitParameter(String name):该方法通过上下文参数名字,获取上下文参数的值
    上下文参数封装到上下文对象中,而上下文对象是全局的,一个应用只有唯一的一个上下文对象。所以上下文参数可以被该应用下任何Servlet或者jsp使用,在JSP中如果要使用上下文参数,可以直接使用application.getInitParameter.
    
    ServletConfig和ServletContext都有一个getInitParameter方法,有什么区别?
        ServletConfig中的getInitParameter获取的是Servlet类的初始化参数。在web.xml中的servlet标记中通过<init-param>设置。Servlet初始化参数只能被该Servlet使用,其他servlet无法使用。ServletContext中的getInitParameter方法,获取的是上下文参数,在web.xml中通过<context-param>设置,可以被该应用下任何一个资源使用。
        
使用的例子:

 

ServletContext context=this.getServletContext();
Integer count=(Integer) context.getAttribute("count");
  <!--
jsp是特殊的Servlet,不好直接获取ServletContext,我们可以直接使用jsp的内置对象application,不需要声明创建
-->
您是第<%=application.getAttribute("count") %>位访问者<br/>
<a href="<%=response.encodeURL("showOwn") %>">显示个人信息</a>
  <context-param>
<param-name>path</param-name>
<param-value>upload</param-value>
</context-param>
       ServletContext context=this.getServletContext();
String path=context.getInitParameter("path");
File file=new File(path);
file.mkdirs();

Context上下文对象(抄书的)的更多相关文章

  1. Asp.Net SignalR Hub中的上下文对象

    Hub中的 Context 使用了集线器后,会发现对比持久连接类少了OnConnectioned这样的事件,事实上是有的.需要我们去override .这下似乎发现了什么问题,记得持久连接类中有con ...

  2. EF上下文对象线程内唯一性与优化

    在一次请求中,即一个线程内,若是用到EF数据上下文对象,就创建一个,这也加是很多人的代码中习惯在使用上下文对象时,习惯将对象建立在using中,也是为了尽早释放上下文对象, 但是如果有一个业务逻辑调用 ...

  3. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(五)

    ASP.NET 上下文对象 ASP.NET 提供了一系列对象用来给当前请求,将要返回到客户端的响应,以及 Web 应用本身提供上下文信息.间接的,这些上下文对象也可以用来回去核心 ASP.NET 框架 ...

  4. Servlet配置对象、上下文对象、多线程问题

    一.Servlet配置对象(ServletConfig):Servlet初始化时,容器调用其init(ServletConfig)方法,传递该对象. 1.获得对象方法: (1).直接使用getServ ...

  5. 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象

    在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...

  6. Asp.Net Core 轻松学-使用MariaDB/MySql/PostgreSQL和支持多个上下文对象

    前言 在上一篇文章中(Asp.Net Core 轻松学-10分钟使用EFCore连接MSSQL数据库)[https://www.cnblogs.com/viter/p/10243577.html],介 ...

  7. Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者

    1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  8. Flask的Context(上下文)学习笔记

    上下文是一种属性的有序序列,为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等. 比如在计算机中,相对于进程而言,上下文就是进 ...

  9. Asp.Net Core获取当前上下文对象

    HttpContext简介 .Net Core中的HttpContext上下文是个抽象类,命名空间为Microsoft.AspNetCore.Http 所在程序集 \netstandard2.0\Mi ...

随机推荐

  1. Java 动态代理机制分析及扩展

    Java 动态代理机制分析及扩展,第 1 部分 王 忠平, 软件工程师, IBM 何 平, 软件工程师, IBM 简介: 本文通过分析 Java 动态代理的机制和特点,解读动态代理类的源代码,并且模拟 ...

  2. Linux多线程之同步2 —— 生产者消费者模型

    思路 生产者和消费者(互斥与同步).资源用队列模拟(要上锁,一个时间只能有一个线程操作队列). m个生产者.拿到锁,且产品不满,才能生产.当产品满,则等待,等待消费者唤醒.当产品由空到不空,通知消费者 ...

  3. JQuery的第一天实战学习

    1.按照下面的工程来建: 2.新建UserVerify.html文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...

  4. 【bzoj2434-阿狸的打字机】AC自动机+fail树+优化

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23083 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一 ...

  5. RHadoop计算平台搭建

     原创文章,转载请注明: 转载自www.cnblogs.com/tovin/p/3824554.html 本文基于CentOS6.4系统介绍基于RHadoop平台的搭建,Hadoop的搭建可以参考ht ...

  6. lintcode:背包问题

    背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为,可以选择的空间. 如果背包的大小 ...

  7. phpstorm安装,破解及使用

    小黑小波比.下载安装phpstorm以及破解 PHPStorm下XDebug配置 phpstorm官方下载地址 PhpStorm 注册码 2.在这输入用户名和注册码,点击OK 破解成功! phpsto ...

  8. gcc 优化选项 -O1 -O2 -O3 -Os 优先级

    http://hi.baidu.com/xiaole10368/item/7cea9b1369cc240db88a1a5c 少优化->多优化: O0 -->> O1 -->&g ...

  9. Win软件私家珍藏-常用软件工具使用总结

    原文:Win软件私家珍藏-常用软件工具使用总结 Windowns常用软件 看图 FastStoneImageViewer 免费软件,好用到爆!没广告!功能齐全!不光能看图,还能修图! Picasa3 ...

  10. MyBatis学习总结_15_定制Mybatis自动代码生成的maven插件

    ==================================================================================================== ...