• Servlet Filter


    Filter,过滤器。动态的拦截请求或者响应,做一些处理

    • 拦截request,处理客户端请求后端时。
    • 拦截response,处理服务端响应客户端之前。

    实现Filter的方式:实现Filter接口。

    Filter配置方式
    1. 在web项目中web.xml进行配置。示例如下:

    <filter>
      <filter-name>过滤器类名称</filter-name>
      <filter-class>过滤器实现类的全限定名</filter-class>
      <init-param>
        <param-name>initParam</param-name>
        <param-value>初始参数结果</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>过滤器类名称</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    2.基于Servlet 3.0的新特性,注解

    @WebFilter(urlPatterns = "/*")
    public class LogFilter implements Filter {
    }

    拦截器方法

    • init
     public void init(FilterConfig fConfig) throws ServletException
    • doFilter
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // place your code here
        写 处理代码
        // pass the request along the filter chain
        chain.doFilter(request, response);
    }
    • destroy
    public void destroy()

    上述,init方法会随着容器启动,而进行初始化,并且只初始化一次。容器重启或者关闭,执行销毁方法destroy。

  • 相关阅读:
    WebBrowser Control(三)Privacy Alert对话框
    工具栏添加控件
    OpenGL(一)Introduction
    WebBrowser Control(四)URL Security Zones
    OpenGL(二)三维变换
    STL(二)Iterators
    string与wstring转换
    vs2005制作安装包
    唉,又是数组越界的问题!
    CString之GetBuffer、ReleaseBuffer
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608397.html
Copyright © 2020-2023  润新知