zoukankan      html  css  js  c++  java
  • Context上下文对象(抄书的)

    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();
  • 相关阅读:
    nginx 点播mp4方法
    NGINX 添加MP4、FLV视频支持模块
    用nginx搭建基于rtmp或者http的flv、mp4流媒体服务器
    obs nginx-rtmp-module搭建流媒体服务器实现直播 ding
    利用nginx搭建RTMP视频点播、直播、HLS服务器
    使用nginx搭建媒体点播服务器
    nginx支持flv MP4 扩展nginx_mod_h264_streaming,nginx-rtmp-module-master,yamdi
    the odb manual
    Zookeeper——启动闪退
    Zookeeper之启动常见错误及解决方法
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5683926.html
Copyright © 2011-2022 走看看