随笔 - 3, 文章 - 152, 评论 - 17, 引用 - 0
数据加载中……

ServletConfig和ServletConfig参数访问.

关于Config参数和Context参数的访问

    虽然在论坛中已经回答过不止一次,但现在看来,真正掌握这两个参数的区别的人5%都不到.
    所以有必要专门写出来,供大家学习.

    我们先来回顾一下各种内置对象的作用范围

    HttpServletRequest,HttpServletResponse:这两个属性的作用范围最小。
    时间上:只是本身请求和应答完成就失效,当然转发是把当前的request对象取出来传给另一
          个资源,其实本身的request对象还是只生存到本次请求结束,response也同样。
    空间上:只能发送请求的客户端有效。

    HttpSession:一次连结到客户端关闭,时间作用范围比上面两个大,空间任用范围相同。

    ServletConfig:从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet
    有效,一个servlet的ServletConfig对象不能被另一个servlet访问。

    ServletContext:对任何servlet,任何人在任何时间都有效,这才是真正全局的对象。

    那么,ServletConfig参数和ServletContext参数到底应该如何使用,如何取得?

    一般来说,对整个应用的配置,为了不使用“硬编码”,应该配置为ServletContext参数,比如字
    符集设定。
    <web-app>
        .................
        <init-param>
            <param-name>charset</param-name> 
            <param-value>GB2312</param-value> 
        </init-param>
        .................
    </web-app>
    注意以上格式只是2。0以后的标准格式,旧容器(引擎)采用服务商自己的格式配置。注意它的
    父元素应该是<web-app>也就是说它是对一个应用作用的。

    而如果只有一个特定的servlet要设定的参数,其它servlet不能共享,应该配置为ServletConfig
    参数,如一个读取附件的servlet要用到绝对目录,而别的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>标签中已经指定了name和class,也就是说只有mail.GetAttServlet这个
    servlet中才能取到path,而别的Servlet是不能取到的。

    那么如何访问这两个对象的参数呢?
    访问ServletConfig参数:
        首先要取得ServletConfig对象,然后调用它的getInitParameter();方法。要访问
    ServletConfig对象,jsp中直接使用config内置对象,但因为你的JSP编译后的servlet一般不会被
    加到web.xml中的,所以一般不会通过jsp来取对本JSP编译后的servlet的配置参数,那么在servlet
    中要得到ServletConfig对象有两种方法:

    在inii()方法中取到:通过init的重载方法传递

    .....
    public class Test extends HttpServlet 
    {
        ServletConfig config;
        public void init(ServletConfig config) throws ServletException {
            this.config = config;
        }
        ..................
    }
    然后在下面的方法中就可以访问config对象。但要注意,为了确保能从构造方法中到到当前servlet的
    config对象,应该调用父类的构造方法:
    .....
    public class Test extends HttpServlet 
    {
        ServletConfig config;
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            this.config = config;
        }
        ..................
    }

    通过getServletConfig()方法直接到时,这样做的好处是不必调手工传递属性,想在任何时候都可
    以得到。

    还有第三种方法,要自己实现一些接口,这里作为一般讨论就不介绍了。

    要访问ServletContext对象,只要从现有的ServletConfig对象getServletContext()就可以了,然后
    调用它的getInitParameter()方法就可以获取它的参数。

    按说:ServletContext对象的作用域比ServletConfig作用域大,为什么要从ServletConfig中到得
    ServletContext对象呢?我个人认为:容器保存了很多个ServletContext对象,请求时容器到底取哪一个
    给你呢?那就取其中包含ServletConfig信息的那个给你,就是说取ServletConfig对象的父级对象。就好
    象HttpSession要从requset中取得一样,就是取那个包含当前requese对象的session对象给你,这只是我
    的个人想法,还没有来得及看具体实现。反正就这么用吧。

posted on 2005-02-12 23:18 阅读(363) 评论(1)  编辑  收藏 所属分类: J2ee

评论

# re: ServletConfig和ServletConfig参数访问.   回复  更多评论   

好象说反了吧?
servletConfig 要比servletContext 范围大:
下面是j2ee api的原话:
ServletConfig:
A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
他是serverlet 容器的环境
servletContext:
Defines a set of methods that a servlet uses to communicate with its servlet container.
...
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
不是说ServletContext被ServletConfig包含.他是每个Servlet的环境.



2005-07-22 11:48 | 东风破

只有注册用户登录后才能发表评论。


网站导航: