JavaWeb-Listener(监听器)梗概
程序员文章站
2022-06-18 11:22:58
...
(Session作用域)监听器:
-----Request和getServletContext(即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...