监听器

监听器:主要是判断ServletContext域HttpSession域和ServletRequest域他们的创建和销毁,属性的变化。

看一下监听器的四个属性。

1.被监听的对象

2.监听事件源对象

3.注册监听器:将对象和事件源绑定

4.响应行为:监听器听到事件源的状态变化时候,所涉及的代码

监听什么呢?比如session的创建和销毁这个监听这两个事件在进行其他的操作就需要用到监听器。

创建session的方法。

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             HttpSession session =request.getSession();
             //将customer放到session中
             Customer customer=new Customer();
             customer.setId("200");
             customer.setName("lucy");
             session.setAttribute("customer", customer);
             System.out.println("customer被放到Session域中了");
             
       }

session创建后的事件

public class MyServletContextListener implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("session创建"+arg0.getSession().getId());
//3.4
	}

关键在于怎么把这两个事件绑定在一起这个就需要web.xml文件。

  <listener>
    <listener-class>attribute.MyServletContextAttributeListener</listener-class>
  </listener>

先总结一下。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值