注:阅读下文之前,建议阅读下JCP的Servlet规范。
1.ServletContext接口的方法
图1 ServletContext接口的方法
要注意的是:一个Web应用,只有一个ServletContext实例。
ServletContext定义了一些方法,目的是让Servlet用ServletContext提供的方法,与容器进行交互。
2.ServletContext的getInitParameter*方法
获取Deployment descriptor中设置的初始化参数,比如我们会在web.xml中设置一些参数,如下List-1
List-1 web.xml中的配置
contextConfigLocation classpath*:/spring-context*.xml
每个Servlet通过ServletContext的下面俩个方法,可以获得List-1中的数据。
■ getInitParameter
■ getInitParameterNames3.ServletContext的get/setAttribute
通过如下*Attribute方法,每个Servlet可以向ServletContext中设置值,之后每个Servlet实例都可以从ServletContext中获取这个值。
■ setAttribute
■ getAttribute ■ getAttributeNames■ removeAttribute