ServletConfig与ServletContext

ServletContext定义了一组servlet与servlet容器之间通信的方法。它的参数-值对(param-value pairs)放在web.xml的<context-param>标签中。

ServletConfig则是servlet容器在servlet初始化时用来给其传递信息的一个配置对象。它的所有初始化参数只能在部署描述符(deployement descriptor)中设置。它的参数-值对(param-value pairs)放在<servlet>的<init-param>标签中。

ServeletContext包含在ServletConfig中。

The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.(ServletContex参数的定义是在整个程序中且独立于各个单独的servlet的,同时也提供给程序中的所有servlet)

而ServletConfig中的参数是只提供给定义它的Servlet的,同一程序中的其他servlet是无法使用的。

也就是说ServletContext相当于全局变量,ServletConfig相当于局部变量。

要访问一个servlet的ServletContext,可以直接通过调用该servlet的getServletContext()方法,也可以先调用servlet的getServletConfig()方法,然后再调用该ServletConfig的getServletContext()方法。

采用后者的原因是:ServletConfig对象拥有ServletContext的引用。所以可以通过getServletConfig()来获得web App的初始值。

几行代码:

 
 

参考资料:

http://topic.csdn.net/u/20080425/11/308777bd-569c-424d-a868-0bb0d80fdabb.html

http://www.sap-img.com/java/difference-between-servletcontext-and-servletconfig.htm

http://java.chinaitlab.com/ServletJsp/533436.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值