`

head first 学习笔记 JSP&Servlet--2

阅读更多
6.servletConfig 和ServletContext之间的区别
  6.1 servletconfig:
      从一个servlet被实例化后,对所有客户端在所有时候访问有效,但仅对本servlet 有效,一个servlet的servletconfig对象不能被另一个servlet访问。
  6.2 servletcontext:
      对所有servlet,所有人在所有时间都有效,这才是真正全局的对象。
  6.3 用处
     如果是整个系统的配置的话,那么就可以设置为servletContext的参数,如系统编码等
    <web-app>
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                        classpath:bme.service.xml
                        /WEB-INF/conf/global.datasource.xml
                        /WEB-INF/conf/*.service.xml /WEB-INF/conf/*.web.xml
                        /WEB-INF/console/conf/*.web.xml
                        /WEB-INF/console/conf/*.service.xml
                        /WEB-INF/frameset/conf/*.web.xml
                        /WEB-INF/frameset/conf/*.service.xml /WEB-INF/*.web.xml
                        classpath:org/codehaus/xfire/spring/xfire.xml
                        classpath:cmp/spring/cmp-spring-integration-startup.xml
                        classpath:iManager/spring/repository_service.xml
                </param-value>
            </context-param>
            ..........
         </web-app>

         若是某个单独servlet的配置项的话,可以设置为servletConfig的参数,如某个读取文件的servlet的目录地址等
         <servlet>
                <servlet-name>getatt</servlet-name>
                <servlet-class>mail.getattservlet</servlet-class>
                <init-param>
                  <param-name>abspath</param-name>
                  <param-value>/usr/mail/ax/axman/maildir/</param-value>
                </init-param>
         </servlet>
         只能这个servlet能获取这些参数,不同servlet之间不能获取
         
    实际上ServletContext的比ServletConfig的使用范围广很多, ServletConfig一般只用于获取Servlet的初始化参数,而ServletContext的作用是与Web Container交互,除了可以用于获取Web Application的全局初始化参数外,还可以获取Servlet的元数据,设置共享属性,获取属性值,获取外部资源等,另外当Web Application使用分布式部署时(在web.xml 中使用distributable元素)ServletContext的作用范围就只有单个虚拟机而不是整个Web Application    

  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics