JavaWeb-Listener(监听器)梗概

(Session作用域)监听器:

-----RequestgetServletContext(即Application)这两个作用域就不讲了----

以下是代码实现的构思:

1.监听Attribute的创建、更改、和移除

2.会话时效request.getSession().setMaxInactiveInterval(10);10的单位是second

首先自己建立一个监听器类,并实现相关的接口(这里使用Web.xml配置,不是注解)

package myListener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyListener implements HttpSessionListener,HttpSessionAttributeListener{

	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		
		String k=se.getName();
        System.out.println("session中键值对(被创建):"+k+","+se.getValue());
		
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
		String k=se.getName();
        System.out.println("session中键值对(被移除):"+k+","+se.getValue());
		
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		String k=se.getName();
        System.out.println("session中键值对(被修改):"+k+","+se.getValue());
		
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("session被创建");
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("session被移除");
		
	}
	
}

在web.xml进行配置(新建的工程看web-app中的version,2.5以上都支持注解,也可以不在xml中进行配置)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>my1012</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>myListener.MyListener</listener-class>
  </listener>
</web-app>

写一个测试类Servlet(这里使用注解,不在web.xml中配置)

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Tonight
 * 
 */
@WebServlet("/test13")
public class Tonight extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public Tonight() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置会话时效
		request.getSession().setMaxInactiveInterval(10);
		
		request.getSession().setAttribute("name","liushuai");
		
		//当该键值对键存在时,再setAttr即等同于replacce
		request.getSession().setAttribute("name","wang");
		
		request.getSession().removeAttribute("name");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

接下来启动tomcat服务器输入:http://localhost:8080/my1012/test13不唯一!按自个工程名适当修改

运行结果:

 

Show End...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值