随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
用于保存所有应用程序中的公有数据。它在服务器启动时自动创建,在服务器停止时销毁。与session对象相比,application对象的生命周期更长,类似于系统的“全局变量”。
一、访问应用程序初始化参数
在web.xml中通过<context-param>标记配置应用程序初始化参数。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
在JSP中可以通过以下两种方式获取:
1、getInitParameter(String name)
<%
String ccl = application.getInitParameter("contextConfigLocation");
out.println(ccl);
%>
2、getInitParameterNames
<%
Enumeration enema = application.getInitParameterNames();
while (enema.hasMoreElements()) {
String name = (String) enema.nextElement();
String value = application.getInitParameter(name);
out.println(name + " " + value);
}
%>
二、管理应用程序环境属性
application设置的属性在整个应用区域都有效,这点与session不同,方法如下: