• 分析LogFilter


    package lee;

    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.*;

    /**
    * Description:
    * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
    * <br/>This program is protected by copyright laws.
    * <br/>Program Name:
    * <br/>Date:
    * @author Yeeku.H.Lee kongyeeku@163.com
    * @version 1.0
    */

    @WebFilter(filterName="log"
    ,urlPatterns={"/*"})
    public class LogFilter implements Filter
    {
    // FilterConfig可用于访问Filter的配置信息
    private FilterConfig config;
    // 实现初始化方法
    public void init(FilterConfig config)
    {
    this.config = config;
    }
    // 实现销毁方法
    public void destroy()
    {
    this.config = null;
    }
    // 执行过滤的核心方法
    public void doFilter(ServletRequest request,
    ServletResponse response, FilterChain chain)
    throws IOException,ServletException
    {
    // ---------下面代码用于对用户请求执行预处理---------
    // 获取ServletContext对象,用于记录日志
    ServletContext context = this.config.getServletContext();
    long before = System.currentTimeMillis();
    System.out.println("开始过滤...");
    // 将请求转换成HttpServletRequest请求
    HttpServletRequest hrequest = (HttpServletRequest)request;
    // 输出提示信息
    System.out.println("Filter已经截获到用户的请求的地址: " +
    hrequest.getServletPath());
    // Filter只是链式处理,请求依然放行到目的地址
    chain.doFilter(request, response);
    // ---------下面代码用于对服务器响应执行后处理---------
    long after = System.currentTimeMillis();
    // 输出提示信息
    System.out.println("过滤结束");
    // 输出提示信息
    System.out.println("请求被定位到" + hrequest.getRequestURI() +
    " 所花的时间为: " + (after - before));
    }
    }

    复制代码

    long before = System.currentTimeMillis();

    long after = System.currentTimeMillis();

    这两句代码定义了doFilter()过滤用户请求的范围。

    HttpServletRequest hrequest = (HttpServletRequest) request;

    HttpServletRequest接口是ServletRequest子接口,HttpServletRequest接口遵循http协议。将请求转换成HttpServletRequest请求。

    System.out.println("Filter 已经截获到用户的请求的地址: "  + hrequest.getSeryletPath() );

    输出提示信息,输出截获到用户的请求的地址。

  • 相关阅读:
    kettle的使用(ETL,数据仓库技术)
    最近工作用到压缩,写一个zip压缩工具类
    JAVA 7新特性——在单个catch代码块中捕获多个异常,以及用升级版的类型检查重新抛出异常
    JAVA反射机制
    guava的使用
    web项目中无法开启或404
    关于request.getServletContext()方法报错的问题
    使用U盘给笔记本重做系统
    sed速查手册
    awk速查手册
  • 原文地址:https://www.cnblogs.com/yc123456/p/8650233.html
Copyright © 2020-2023  润新知