注:阅读下文以前,建议阅读下JCP的Servlet规范。web
图1 ServletContext接口的方法spring
要注意的是:一个Web应用,只有一个ServletContext实例。spa
ServletContext定义了一些方法,目的是让Servlet用ServletContext提供的方法,与容器进行交互。code
获取Deployment descriptor中设置的初始化参数,好比咱们会在web.xml中设置一些参数,以下List-1xml
List-1 web.xml中的配置blog
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/spring-context*.xml </param-value> </context-param> <context-param>
每一个Servlet经过ServletContext的下面俩个方法,能够得到List-1中的数据。接口
■ getInitParameter
■ getInitParameterNamesip
经过以下*Attribute方法,每一个Servlet能够向ServletContext中设置值,以后每一个Servlet实例均可以从ServletContext中获取这个值。rem
■ setAttribute
■ getAttribute
■ getAttributeNamesget
■ removeAttribute