04-过滤器与监听器(一)
一大早的起来看看视频,一过程的打瞌睡,要了老命,休息会儿开始做跟着敲代码,才醒了过来,具体视频是aliyun的java学习噢,纯手工笔记哈哈哈:
(一)-Listener,监听器
01- 概念:
- 一个接口,内容可自定义;
- 需要注册,例如注册在按钮上;
- 其中的方法,会在特殊事件发生时被 调用;
观察者:
- 事件源;
- 事件;
- 监听器;
事件源(三大域):
- ServletContext
- 生命周期监听;servletContextListener
- 两个方法,创建时,销毁时;
- 属性监听;ServletContextAttributeListener
- 三个方法,创建时,替换属性时,移除属性时;
- 生命周期监听;servletContextListener
- HttpSession(类比)
- ServletRequest(类比)
事件对象:
- ServletContextEvent:servletContext,getServletContext
- HttpSessionEvent:HttpSession getSession;
- ServletRequest:
- ServletContext getServletContext();
- ServletRequest getServletReq();
02- 生命周期监听器
package Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class servConListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Listener Initialized;");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Listener Destroyed;");
}
}
在xml中配置:
<listener>
<listener-class>Listener.servConListener</listener-class>
</listener>
可以在监听器中存放一些在tomcat启动时就要完成的代码;
03- 属性监听
package Listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class servConAttrListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("application add:"+scab.getName()+","+scab.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("the old value:"+scab.getValue()+
"the new value:"+
scab.getServletContext().getAttribute(scab.getName())
);
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
}
}// 运行后:
<!-- 在jsp 中加入以下的代码:修改map中 存储的内容 -->
<%
application.setAttribute("listener_name","listener_jason");
%>
<% System.out.println(application.getAttribute("listener_name")); %>
上面为效果
04- 感知监听:(都与HttpSession相关)
- 在JavaBean中添加,不加到三大域中;
- ps:这两个监听器都不需要在web.xml 中注册;
package Listener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class User implements HttpSessionBindingListener {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("running the add&valueBound method here");
}
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("running the remove&valueUnbound method here");
}
}
<!-- add.jsp -->
<!--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/6/21
Time: 14:47
To change this template use File | Settings | File Templates.
-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<%
Listener.User user = new Listener.User();
session.setAttribute("user",user);
%>
</body>
</html>
05- session的序列化
当服务器关闭后:
那,如何去掉这个序列化呢:在tomcat 配置文件中,的context.xml 中设置添加:
<Manager pathname=""/>
关闭服务器再重启后,便不会回复数据了;
06- session 的钝化和活化
并没有关闭服务器,将session 存入磁盘,和从磁盘读取两个操作;
<context>
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleSwap="1">
<Store className="org.apache.catalina.session.FileStore"
directory="mysession"/>
</Manager>
</context>
放在需要的项目下或者tomcat配置目录下的context.xml(对所有项目有效) 中;
一分钟后出现:(这个配置是放在tomcat的context文件下,)途中遇到了问题,直接放在项目context下不行,服务器重启后,里面的配置信息也不见了,mysession文件夹也没有生成;
07- HttpSessionActivationListener
package Listener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class Activation implements HttpSessionActivationListener,java.io.Serializable {
// 注意这里不同于字符串,需要自己设置接口序列化
@Override
public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
System.out.println("Passivate");
}
@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
System.out.println("Activate");
}
}
<!-- src.jsp -->
<% session.setAttrivute("activate_",new Listener.Activation()) %>
<!-- target.jsp -->
<% out.print(session.getAttribute("activate_")); %>
08- 总结
监听器部分掌握起来还是比较容易,虽然还是有少许问题没有解决呢,待解决,代码部分希望能看懂哇。