当前位置: 代码迷 >> 综合 >> SERVLETJSP Unit08: 过滤器、监听器
  详细解决方案

SERVLETJSP Unit08: 过滤器、监听器

热度:14   发布时间:2023-12-11 15:02:44.0

一.过滤器

1.过滤器的作用

  • 用来处理项目中的公共的业务

  • 记录日志,过滤敏感词,登录检查

公共业务:很多请求都需要进行处理的业务

2.使用步骤

  • 创建一个类,实现Filter接口

  • 在web.xml中配置此类

3.小案例

这里写图片描述

4.登录检查

这里写图片描述

5.参数说明

这里写图片描述

二.监听器

1.什么是监听器

  • 监听器用来监听事件,当某事件发生时,它会通知服务器

  • 它监听的目标是一些对象

2.监听器的分类(接口)

1) request

  • 创建与销毁:ServletRequestListener

  • 数据的变化:ServletRequestAttributeListener

2) session

  • 创建与销毁:HttpSessionListener

  • 数据的变化:HttpSessionAttributeListener

3) context

  • 创建与销毁:ServletContextListener

  • 数据的变化:ServletContextAttributeListener

3.开发步骤

  • 创建一个类,实现对应的监听器接口

  • 在web.xml中声明此类


案例

这里写图片描述

AddCostServlet.java

package web;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class AddCostServlet extends HttpServlet {
    @Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {System.out.println("增加资费");}}

FindCostServlet.java

package web;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class FindCostServlet extends HttpServlet {
    @Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {System.out.println("查询资费");req.setAttribute("name", "zhangsan");}}

GuolvFilter.java

package web;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class GuolvFilter implements Filter {
    public void destroy() {System.out.println("销毁GuolvFilter");}public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {System.out.println("在前面过滤敏感词");chain.doFilter(req, res);System.out.println("在后面过滤敏感词");}String words;public void init(FilterConfig cfg) throws ServletException {System.out.println("初始化GuolvFilter");words = cfg.getInitParameter("words");System.out.println("words:"+words);}}

LogFilter.java

package web;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class LogFilter implements Filter {
    public void destroy() {System.out.println("销毁LogFilter");}public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {System.out.println("在前面记日志");//请求继续,向下执行chain.doFilter(req, res);System.out.println("在后面记日志");}public void init(FilterConfig arg0) throws ServletException {System.out.println("初始化LogFilter");}}

MyListener.java

package web;import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;public class MyListener implements ServletRequestListener,ServletRequestAttributeListener {
    public void requestDestroyed(ServletRequestEvent e) {//request销毁时调用System.out.println("销毁request");System.out.println(e.getServletRequest());}public void requestInitialized(ServletRequestEvent arg0) {//request创建时调用System.out.println("创建request");}public void attributeAdded(ServletRequestAttributeEvent arg0) {System.out.println("向request添加数据");}public void attributeRemoved(ServletRequestAttributeEvent arg0) {// TODO Auto-generated method stub}public void attributeReplaced(ServletRequestAttributeEvent arg0) {// TODO Auto-generated method stub}}

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" version="2.5"><display-name>jsp5</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><servlet><servlet-name>findCost</servlet-name><servlet-class>web.FindCostServlet</servlet-class></servlet><servlet-mapping><servlet-name>findCost</servlet-name><url-pattern>/findCost</url-pattern></servlet-mapping><servlet><servlet-name>addCost</servlet-name><servlet-class>web.AddCostServlet</servlet-class></servlet><servlet-mapping><servlet-name>addCost</servlet-name><url-pattern>/addCost</url-pattern></servlet-mapping><filter><filter-name>log</filter-name><filter-class>web.LogFilter</filter-class></filter><filter-mapping><filter-name>log</filter-name><!-- 声明Filter对哪些请求路径有效,即用户在访问哪些路径时Tomcat调用此Filter. --><url-pattern>/*</url-pattern></filter-mapping><!-- 多个Filter其顺序以mapping的配置顺序为准 --><filter><filter-name>guolv</filter-name><filter-class>web.GuolvFilter</filter-class><init-param><param-name>words</param-name><param-value>靠,TMD,NND</param-value></init-param></filter><filter-mapping><filter-name>guolv</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 注册监听器,Tomcat会在相应事件发生时,自动调用此监听器的对应的方法. --><listener><listener-class>web.MyListener</listener-class></listener></web-app>