【Servlet】Listener监听器接口与Filter过滤器接口
一、Listener接口
1、介绍
- 一组来自于Servlet规范下的接口,共有8个接口。在Tomcat存在servlet-api.jar包
- 监听器接口需要由开发人员钦此实现,Http服务器提供jar包并没有对应的实现类
- 监听器接口用于监控【作用域对象生命周期变化时刻】以及【作用域对象共享数据变化时刻】
2、作用域对象
3、监听器接口实现类开发规范(步骤)
(1)根据监听的实际情况,选择对应监听器接口进行实现
(2)重写监听器接口声明【监听事件处理方法】
(3)在web.xml文件将监听器接口实现类注册到Http服务器
4、ServletContextListener接口
5、ServletContextAttributeListener接口
6、实验案例
-
文件目录结构 -
OneServlet.java package com.Etui.Controller;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "OneServlet", value = "/one")
public class OneServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
servletContext.setAttribute("key1", 123);
servletContext.setAttribute("key1", 456);
servletContext.removeAttribute("key1");
}
}
-
OneListener.java package com.Etui.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class OneListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext全局作用域被创建");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext全局作用域被销毁");
}
}
-
TwoListener.java package com.Etui.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class TwoListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("已向全局作用域添加属性!");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("已修改全局作用域中的属性!");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("已从全局作用域移除属性!");
}
}
-
web.xml <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>com.Etui.listener.OneListener</listener-class>
</listener>
<listener>
<listener-class>com.Etui.listener.TwoListener</listener-class>
</listener>
</web-app>
二、Filter接口
1、介绍
- Filter位于客户端和处理程序之间,能够对请求和响应进行检查和修改,通常将请求拦截后进行一些通用的操作,例如:过滤敏感词汇、统一字符编码和实施安全控制等。
- Filter是来自于Servlet规范下的接口,在Tomcat中存在于servlet-api.jar包
- Filter接口实现类通常由开发人员负责提供,Http服务器不负责提供
- Filter接口在Http服务器调用资源文件之前,对Http服务器进行拦截
2、具体作用
- 拦截Http服务器,帮助Http服务器检测当前请求合法性
- 拦截Http服务器,对当前请求进行增强操作
3、Filter接口实现类开发步骤
(1)创建一个Java类实现Filter接口
(2)重写Filter接口中的doFilter方法
(3)web.xml将过滤器接口实现类注册到Http服务器
4、Filter拦截地址格式
-
命令格式 <filter-mapping>
<filter-name>oneFilter</filter-name>
<url-pattern>拦截地址</url-pattern>
</filter-mapping>
-
命令作用:拦截地址通知Tomcat在调用何种资源文件之前需要调用OneFilter过滤进行拦截。 -
要求Tomcat在调用某一个具体文件之前,来调用OneFilter拦截 <url-pattern>/img/mm.jpg</url-pattern>
-
要求Tomcat在调用某一个文件夹下所有的资源文件之前,来调用OneFilter拦截 <url-pattern>/img/*</url-pattern>
-
要求Tomcat在调用任意文件夹下某种类型文件之前,来调用OneFilter拦截 <url-pattern>*.jpg</url-pattern>
-
要求Tomcat在调用网站中任意文件时,来调用OneFilter拦截 <url-pattern>/*</url-pattern>
5、实验案例——Filter在登录验证中的体现
-
由于案例仅体现Filter在登录验证中的作用,故仅展示Filter相关的源码。 -
项目目录结构如下: -
UserLoginServlet.java package com.powernode.controller;
import com.powernode.dao.UserDao;
import com.powernode.entity.Users;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class UserLoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
UserDao dao = new UserDao();
Boolean result = dao.find(userName, password);
if(result) {
request.getSession();
response.sendRedirect("/myWeb/index.html");
} else {
response.sendRedirect("/myWeb/login_error.html");
}
}
}
-
OneFilter.java package com.powernode.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.LogRecord;
public class OneFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String uri = request.getRequestURI();
if(uri.indexOf("login")!=-1 || "/myWeb/".equals(uri)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
HttpSession session = request.getSession(false);
if (session != null) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
request.getRequestDispatcher("/login_error.html").forward(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
-
web.xml <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>OneFilter</filter-name>
<filter-class>com.powernode.filter.OneFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OneFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
-
运行结果
Over!
|