Servlet的Listener配置

Servlet的Listener配置

Web应用在启动、停止,用户session的创建、销毁,请求的到达、结束都会发生着各种各样的事件,我们可以根据根据这些事件去执行一些操作。如应用启动时,可以初始化项目配置,向数据库写入基础数据等。
Servlet API提供了很多Listener(监听器)去监听Web应用的事件,通过实现这些监听器接口比较方便做一些操作。
常用的Web监听器如下:
  • ServletContextListener:监听Web应用的启动和停止;
  • ServletContextAttributeListener:监听application域中属性的改变;
  • ServletRequestListener:监听请求的创建与销毁;
  • ServletRequestAttributeListener:监听request域中属性的改变;
  • HttpSessionListener:监听session的创建与销毁;
  • HttpSessionAttributeListener:监听session域中属性的改变;

1.使用ServletContextListener示例:

监听Web应用的创建与销毁,应用创建时获取初始化参数appName,并在控制台打印
1.1Listener配置
(1).方式一:使用@WebListener注解
@WebListener
public class MyServletContextListener implements ServletContextListener {...}
(2).方式二:web.xml中使用<litener>标签
<listener>
  	<listener-class>cn.edu.njit.listener.MyServletContextListener</listener-class>
</listener>
1.2初始化参数配置
在web.xml中使用<context-param>标签
<context-param>
  	<param-name>appName</param-name>
  	<param-value>test_servlet</param-value>
</context-param>
1.3MyServletContextListener
实现ServletContextListener接口,重写contextInitialized(ServletContextEnent sce)和contextDestroyed(ServletContextEnent sce)方法
public class MyServletContextListener implements ServletContextListener {

	/**
	 * 应用启动时触发
	 * @param sce
	 * */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("应用初始化。。。");
		
		ServletContext application = sce.getServletContext();
		
		// 获取<context-param>参数
		String appName = application.getInitParameter("appName");
		System.out.println("appName:" + appName);
	}
	
	
	/**
	 * 应用关闭时触发
	 * @param sce
	 * */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("应用关闭。。。");
	}
}
1.4测试结果
(1).应用启动

(2).应用关闭




2.使用ServletContextAttributeListener示例

Listener配置方式同1.1类似
2.1MyServletContextAttributeListener
实现ServletContextAttributeListener接口,监听属性的添加、修改、移除事件
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
	
	// 程序把属性存入application时触发该方法
	@Override
	public void attributeAdded(ServletContextAttributeEvent sce) {
		String name = sce.getName();
		Object value = sce.getValue();
		
		System.out.println("application域中添加了新的属性,name=" + name + ",value=" + value);
	}


	// 程序把属性从application移除时触发该方法
	@Override
	public void attributeRemoved(ServletContextAttributeEvent sce) {
		String name = sce.getName();
		Object value = sce.getValue();
		
		System.out.println("application域中移除了属性,name=" + name + ",value=" + value);
	}


	// 程序替换application中的属性时触发该方法
	@Override
	public void attributeReplaced(ServletContextAttributeEvent sce) {
		String name = sce.getName();
		Object value = sce.getValue();
		
		System.out.println("application域中修改了属性,name=" + name + ",value=" + value);
	}

}
2.2测试的Servlet
@WebServlet(
		name = "listenerServlet", 
		urlPatterns = "/listener/*"
		)
public class ListenerServlet extends HttpServlet {
	private static final long serialVersionUID = 9200277932328016173L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		String methodName = uri.substring(uri.lastIndexOf("/") + 1);
		
		if (methodName.equals("applicationAdded")) {// application域中属性添加、修改
			applicationAdded(request, response);
		}else if(methodName.equals("applicationRemoved")){// application域中属性移除
			applicationRemoved(request, response);
		}
		
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
		
	
	/**
	 * Description:application域中属性添加、更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @date 2017年4月26日
	 * <p>Description:</p>
	 */
	public void applicationAdded(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application = this.getServletContext();
		application.setAttribute("currentDate", new Date());
	}
	
	/**
	 * Description:application域中属性移除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @date 2017年4月26日
	 * <p>Description:</p>
	 */
	public void applicationRemoved(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application = this.getServletContext();
		application.removeAttribute("currentDate");
	}
	
}
2.3测试结果
(1).属性添加

(2).属性修改

(3).属性移除

3.注

(1).通过监听应用的启动、停止事件可以实现一些操作,如项目启动时加载一些必要的数据,向数据库插入数据等,停止时则可以释放一些资源或者删除测试信息;
(2).属性的监听中,执行修改时,获取的值是修改之前的;
(3).通过对session的创建与销毁也可以简单实现在线用户的记录;也可以对request请求的创建与销毁比较详细记录在线用户数以及监控用户访问的页面;








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值