博客
关于我
filter&listener&邮箱
阅读量:117 次
发布时间:2019-02-25

本文共 2800 字,大约阅读时间需要 9 分钟。

Filter技术

1.1 Filter的定义

Filter是Java类,实现了javax.servlet.Filter接口。它用于对请求资源(如JSP、Servlet、HTML等)进行过滤,通常在服务器端运行,优先于请求资源处理。

1.2 Filter的作用

  • 对目标资源进行过滤。
  • 常见应用场景:
    • 登录权限检查
    • 解决网站乱码问题
    • 过滤敏感字符

Filter的配置方式

通过配置文件(xml方式)

  • 创建一个类实现Filter接口。
  • 在web.xml中配置Filter。
  • FilterDemo01
    com.itheima.web.filter.FilterDemo01
    FilterDemo01
    /demo01

    通过注解方式

  • 创建一个类实现Filter接口。
  • 在类上添加@WebFilter注解,指定拦截路径。
  • @WebFilter("/demo02")
    public class FilterDemo02 implements Filter {
    // doFilter方法实现
    }

    Filter的生命周期

    • init(FilterConfig filterConfig): 初始化,调用一次。
    • doFilter(ServletRequest req, ServletResponse resp, FilterChain chain): 处理请求,调用链滤过一次。
    • destroy(): 销毁,调用一次。

    Filter的常见配置

    • url-pattern: 定义过滤路径。
    • dispatcherTypes: 指定过滤方式:
      • DispatcherType.REQUEST: 默认值,处理浏览器请求和重定向。
      • DispatcherType.FORWARD: 只处理转发请求。

    Filter链

    过滤器链的作用是实现多个过滤器的协作。过滤器执行顺序根据配置文件或注解的顺序决定。

    非法字符过滤

    ###需求分析

    • 对用户发布的言论进行非法字符检查。
    • 提示非法言论。

    ###实现步骤

  • 创建非法字符列表。
  • 检查用户输入的言论是否包含非法字符。
  • 如果包含,提示用户;如果没有,放行。
  • ###代码实现

    @WebFilter("/*")
    public class IllegalFilter implements Filter {
    private List
    wordList = new ArrayList<>();
    public void init(FilterConfig config) throws ServletException {
    InputStream is = config.getServletContext().getResourceAsStream("IllegalWords.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
    String line = null;
    while ((line = reader.readLine()) != null) {
    wordList.add(line);
    }
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    String message = request.getParameter("message");
    if (message != null) {
    for (String word : wordList) {
    if (message.contains(word)) {
    message = message.replace(word, "***");
    response.getWriter().print("您发布的言论是:" + message);
    return;
    }
    }
    }
    chain.doFilter(request, response);
    }
    }

    Listener的应用

    ###ServletContext监听器

    • 用于监听ServletContext的创建和销毁。
    • 应用场景:
      • 初始化工作(如加载配置文件)。
      • 服务器启动时创建,服务器关闭时销毁。

    ###代码实现

    public class ServletContextLis implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    System.out.println("ServletContext 创建了... 555555");
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    System.out.println("ServletContext 销毁了... 444444");
    }
    }
    com.itheima.web.listener.ServletContextLis

    邮箱配置

    ###邮件服务器选择

    • 企业邮箱(如163、QQ、Gmail)。
    • 自己搭建邮件服务器(如Sendmail、Postfix)。

    ###邮件软件安装

    • 服务端:安装邮件服务器。
    • 客户端:安装邮件客户端(如Foxmail、Outlook)。

    转载地址:http://ujk.baihongyu.com/

    你可能感兴趣的文章
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>