自我理解:
1)ServletContext接口:全局变量,所以servlet都能访问
(a)web.xml中的配置方法:
<context-param>
<param-name>user</param-name>
<param-value>123</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>abc</param-value>
</context-param>
(b)在servlet中取值:
ServletContext context=this.getServletContext();
String user = context.getInitParameter("user");
String password=context.getInitParameter("password");
2)SeervletConfig接口:局部变量,只是针对具体某个servlet
(a) xml中的配置方法:
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>com.lore.fei.First</servlet-class>
<init-param>
<param-name>123</param-name>
<param-value>abc</param-value>
</init-param>
</servlet>
(b)在servlet中取值:
ServletConfig config = this.getServletConfig();
String servletName=config.getServletName(); //取得servlet的类名
Enumeration<String> em = config.getInitParamerName();
//使用下面的方法打印在控制台
while(em.hasMoreElements()){ //hasMoreElements()方法: 测试此枚举是否包含更多的元素。
System.out.println(config.getInitPatameter(em.nextElement()));
}