提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
一、监听器Listener
1.什么是监听器
监听器就是监听某个域对象的的状态变化的组件
监听器的相关概念:
事件源:被监听的对象(三个域对象 request、session、servletContext)
监听器:监听事件源对象事件源对象的状态的变化都会触发监听器(6+2)
注册监听器:将监听器与事件源进行绑定
响应行为:监听器监听到事件源的状态变化时所涉及的功能代码(程序员编写代码)
2.监听器有哪些
第一维度按照被监听的对象划分:ServletRequest域、HttpSession域、ServletContext域
第二维度按照监听的内容分:监听域对象的创建与销毁的、监听域对象的属性变化的
3.监听器术语
- 事件源
- 监听器
- 事件源与监听器绑定
- 事件
4.监听三大域对象的创建与销毁的监听器
监听器的编写步骤(重点):
编写一个监听器类去实现监听器接口
覆盖监听器的方法
需要在web.xml中进行配置—注册
监听ServletContext域的创建与销毁的监听器ServletContextListener
Servlet域的生命周期
何时创建:服务器启动创建
何时销毁:服务器关闭销毁
ServletContextListener监听器的主要作用
初始化的工作:初始化对象、初始化数据(加载数据库驱动、连接池的初始化)
加载一些初始化的配置文件(spring的配置文件)
任务调度(定时器—Timer/TimerTask)
实例应用:spring加载初始化参数
整体流程:create包下创建MyServletContextListener类实现ServletContextListener接口并覆盖方法public void contextInitialized(ServletContextEvent arg0)监听context域对象的创建;覆盖方法public void contextDestroyed(ServletContextEvent arg0)监听context域对象的销毁。并在web.xml中通过中的书写监听器全类名配置。
实现任务调度(即定时器)的思路:使用Timer类对象的scheduleAtFixedRate(task,firstTime,period)方法第一个参数书写TimerTask()的匿名内部类重写run方法从而实现调度任务内容;firstTime是Date类型对象,我们可以设置成当前晚上12点;period是间隔执行时间(单位毫秒),我们可以设置成24小时。从而实现从当前晚上12点开始,每24小时都执行对应的调度任务。
实例代码:
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 注册监听器 -->
<listener>
<listener-class>create.MyServletContextListener</listener-class>
</listener>
<display-name>WEB23_LISTENER</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
//MyServletContextListener.java
package create;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener{
//监听context域对象的创建
@Override
public void contextInitialized(ServletContextEvent arg0) {//参数可以获得被监听的对象
// ServletContext servletContext1 = arg0.getServletContext();//返回值是被监听的对象
// Object servletContext2 = arg0.getSource();//等同于arg0.getServletContext(),获得的是Object类型,但实际上也是获得被监听的对象
// System.out.println("context创建了...");
//开启一个计息任务调度——每天晚上12点计息一次
Timer timer = new Timer();
//task任务,firstTime第一次执行的时间,period间隔执行的时间(单位毫秒)
//timer.scheduleAtFixedRate(task, firstTime, period);
//测试demo:从服务器启动开始每隔5秒打印“银行计息了”
// timer.scheduleAtFixedRate(new TimerTask() {
// @Override
// public void run() {
// System.out.println("银行计息了...");
// }
// }, new Date(), 5000);
//实际银行计息业务
//1.起始时间,定义成晚上12点
//2.间隔时间:24小时
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentTime = "2018-08-15 00:00:00";
Date date = null;
try {
date = format.parse(currentTime);
} catch (ParseException e) {
e.printStackTrace();
}
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("银行计息了...");
}
}, date, 24*60*60*1000);
}
//监听context域对象的销毁
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("context销毁了...");
}
}
问题创建session
访问HTML不会
访问JSP会默认调用get.session
访问Servlet不会
stop servers会是session序列化成文件,启动服务器会反序列化
terminate不会序列化session,全部销毁
监听Httpsession域的创建于销毁的监听器HttpSessionListener
HttpSession对象的生命周期
何时创建:第一次调用request.getSession时创建
何时销毁:服务器关闭销毁、session过期(默认30分钟,修改默认的30分钟是在Tomcat的web.xml,修改当前项目的过期时间是在自己项目的web.xml中)、手动销毁
HttpSessionListener监听器的主要作用:
由于每次访问网站都会默认创建session对象(jsp页面中page指令中的session属性默认为true,即被访问时创建session),可以用于计数网站访问过的人
监听ServletRequest域创建与销毁的监听器ServletRequestListener
ServletRequest的生命周期
创建:每一次请求都会创建request
销毁:对请求做出响应后结束
用法同上,用处不是很大,此处省略。
问题创建request
访问HTML会
访问JSP会
访问Servlet会
5.监听三大域对象的属性变化的
域对象的通用的方法:属性添加、移除、更替
setAttribute(name,value)
触发添加属性的监听器的方法
触发修改属性的监听器的方法
getAttribute(name)
removeAttribute(name)
触发删除属性的监听器的方法
ServletContextAttibuteListener监听器
整体使用流程:定义类MyServeltContextAttributeListener实现ServeltContextAttributeListener接口并覆盖public void attributeAdded(ServletContextAttributeEvent arg0) ;public void attributeRemoved(ServletContextAttributeEvent arg0);public void attributeReplaced(ServletContextAttributeEvent arg0)三个方法。在attributeAdded方法中的参数传递的ServletContextAttributeEvent实例对象中,调用getName方法获取放到域中的name,调用getValue方法获取放到域中的value;在attributeRemoved方法中的参数传递的ServletContextAttributeEvent实例对象中,调用getName方法获取被删除的域中的name,调用getValue方法获取被删除的域中的value;在attributeReplaced 方法中的参数传递的ServletContextAttributeEvent实例对象中,调用getName方法获取修改前的域中的name,调用getValue方法获取修改前的域中的value。并在web.xml中注册监听器。
实例代码:
//MyServletContextAttributeLister.java
package attribute;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener{
@Override
public void attributeAdded(ServletContextAttributeEvent arg0) {
//添加属性时的监听方法
System.out.println(arg0.getName());//获得放到域中的name
System.out.println(arg0.getValue());//获得放到域中的value
}
@Override
public void attributeRemoved(ServletContextAttributeEvent arg0) {
//移除属性时的监听方法
System.out.println(arg0.getName());//删除的域中的name
System.out.println(arg0.getValue());//删除的域中的value
}
@Override
public void attributeReplaced(ServletContextAttributeEvent arg0) {
//修改属性时的监听方法
System.out.println(arg0.getName());//获得修改前的域中的name
System.out.println(arg0.getValue());//获得修改前的域中的value
}
}
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<listener>
<listener-class>attribute.MyServletContextAttributeListener</listener-class>
</listener>
<display-name>WEB23_LISTENER</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>TestMyServletContextAttributeListener</display-name>
<servlet-name>TestMyServletContextAttributeListener</servlet-name>
<servlet-class>attribute.TestMyServletContextAttributeListener</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestMyServletContextAttributeListener</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
</web-app>
//用于测试的接口TestMyServletContextAttributeListener.java
package attribute;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestMyServletContextAttributeListener extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//向context域中存数据
context.setAttribute("name", "tom");
//改context数据
context.setAttribute("name", "lucy");
//删context数据
context.removeAttribute("name");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
当调用context.setAttribute(“name”, “tom”)时打印name tom;当调用context.setAttribute(“name”, “lucy”)时打印name tom;当调用context.removeAttribute(“name”)时打印name lucy。
HttpSessionAttributeListener监听器(同上)
ServletRequestAriibuteListenr监听器(同上)
6.与session中的绑定的(java类)对象相关的监听器(对象感知监听器)
即将要被绑定到session中的对象有几种状态
绑定状态:就一个对象被放到session域中(setAttribute)
解绑状态:就是这个对象从session域中移除了(removeAttribute)
钝化状态:是将session内存中的对象持久化(序列化)到磁盘
活化状态:就是将磁盘上的对象再次恢复到session内存中(注意对象必须实现Serializable
接口)
绑定与解绑的监听器HttpSessionBindingListener(绑在对象上的,且不用在web.xml配置)
流程:创建对象实现HttpSessionBindingListener 接口中的public void valueBound(HttpSessionBindingEvent arg0)(绑定的方法,将对象放到session时触发)和public void valueUnbound(HttpSessionBindingEvent arg0)(解绑的方法,将对象从session中移除时触发)。
实例代码:
//Person.java创建Person类
package domian;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Person implements HttpSessionBindingListener{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}
public Person() {
super();
}
@Override
public void valueBound(HttpSessionBindingEvent arg0) {
// 绑定的方法,将person对象放到session时触发
System.out.println("Person被绑定");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
// 解绑的方法,将person对象从session移除时触发
System.out.println("Person被解绑");
}
}
//TestPersonBindingServlet.java
package domian;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestPersonBindingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//将person对象绑定到session中
Person p = new Person("100","jack");
session.setAttribute("person",p);
//将person对象从session中解绑
session.removeAttribute("person");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
当访问接口时,代码执行到session.setAttribute(“person”,p);时输出Person被绑定;代码执行到session.removeAttribute(“person”);时输出Person被解绑。
钝化与活化的监听器HttpSessionActivationListener 【重要:用于服务器优化】
默认:实现了HttpSessionActivationListener(覆盖其中的public void sessionDidActivate(HttpSessionEvent arg0)和public void sessionWillPassivate(HttpSessionEvent arg0)方法)和Serializable接口(注意必须实现Serializable接口)的对象,被放入到session后,当服务器stop时,session会被钝化到work/catalina/localhost中命名为SESSIONS.ser;当服务器start时,session会被活化到session域中。
手动设置钝化时间和存储位置(通过配置文件指定对象钝化时间—对象多长时间不用被钝化):
在META-INF下创建一个context.xml
context.xml中的代码:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- maxIdleSwap:session中的对象多长时间不使用就钝化(单位:分钟),注意钝化和销毁的概念完全不同 -->
<!-- directory:钝化后的对象的文件写到磁盘的哪个目录下配置钝化的对象文件在 work/catalina/localhost/钝化文件 -->
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"><!-- Manager是处理内容的对象 -->
<Store className="org.apache.catalina.session.FileStore" directory="storeFile" /><!-- Store是做存储的对象 -->
</Manager>
</Context>
实例代码:
//Customer.java
package domian;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class Customer implements HttpSessionActivationListener,Serializable{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + "]";
}
public Customer(String id, String name) {
super();
this.id = id;
this.name = name;
}
public Customer() {
super();
}
@Override
public void sessionDidActivate(HttpSessionEvent arg0) {
//活化
System.out.println("customer被活化了");
}
@Override
public void sessionWillPassivate(HttpSessionEvent arg0) {
//钝化
System.out.println("customer被钝化了");
}
}
//TestCustomerActiveServlet.java 用于将customer对象放至session中
package domian;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestCustomerActiveServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//将customer放到session中
Customer customer = new Customer("200","lucy");
session.setAttribute("customer", customer);
System.out.println("customer被放到session中");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//TestCustomerActiveServlet2 用于测试session对象是否被活化
package domian;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestCustomerActiveServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//从session域中获取customer
Customer customer = (Customer)session.getAttribute("customer");
System.out.println(customer.getName());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
面试题:当用户很多时,怎样对服务器进行优化?
通过session的钝化与活化,将长期不操作的session内存中的对象持久化到磁盘,为服务器腾出空间,等到用户再次开始操作时将磁盘上的对象恢复到session内存中。
二、过滤器(Filter)
1.引入库
过滤器实际上就是对web资源进行拦截,做一些处理后再交给下一个过滤器或servlet处理。通常都是用来拦截request进行处理的,也可以对返回的response进行拦截处理
应用场景
自动登录
统一设置编码格式
访问权限控制
敏感字符过滤等
需要注意的是
过滤器是在服务器启动时就会创建的,只会创建一个实例,常驻内存,也就是说服务器一启动就会执行Filter的init(FilterConfig config)方法.
当Filter被移除或服务器正常关闭时,会执行destroy方法
创建完成后默认代码,可以看到,CharsetFilter实现了Filter接口,实现了3个方法。3个方法的作用已经在注释中写清楚了。
package filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "CharsetFilter")
public class CharsetFilter implements Filter {
public void destroy() {
/*销毁时调用*/
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
/*过滤方法 主要是对request和response进行一些处理,然后交给下一个过滤器或Servlet处理*/
chain.doFilter(req, resp);//交给下一个过滤器或servlet处理
}
public void init(FilterConfig config) throws ServletException {
/*初始化方法 接收一个FilterConfig类型的参数 该参数是对Filter的一些配置*/
}
}
多个Filter的执行顺序
在我们的请求到达Servle之间是可以经过多个Filter的,一般来说,建议Filter之间不要有关联,各自处理各自的逻辑即可。这样,我们也无需关心执行顺序问题。
如果一定要确保执行顺序,就要对配置进行修改了,执行顺序如下
- 在web.xml中,filter执行顺序跟的顺序有关,先声明的先执行
- 使用注解配置的话,filter的执行顺序跟名称的字母顺序有关,例如AFilter会比BFilter先执行
- 如果既有在web.xml中声明的Filter,也有通过注解配置的Filter,那么会优先执行web.xml中配置的Filter
过滤器相关配置
的配置
- 完全路径匹配:以/开始,/aaa,/bbb
- 目录匹配:以/开始,以结束,/aaa/
- 扩展名匹配:以开始,.jsp
的配置
的配置
- REQUEST默认拦截请求
- FORWARD转发
- INCLUDE页面包含拦截
- ERROR出现错误进行拦截