一:自定过滤器java书写
二: web.xml配置
一:自定过滤器java书写
自定义过滤器,其实就是实现接口javax.servlet.Filter中的三个方法:init,doFilter,destroy
package com.test; import javax.servlet.*; import java.io.IOException; public class FilterStudy implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("asplover 自定义过虑器初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("asplover 自定义过滤器要做的事情"); System.out.println(servletRequest); } @Override public void destroy() { System.out.println("asplover 自定义过虑器销毁"); } }
二: web.xml配置
<!-- 过滤器配置标签有两个filter和filter-mapping--> <filter> <filter-name>filterStudy</filter-name> <filter-class>com.test.FilterStudy</filter-class> <init-param> <param-name>paraName2</param-name> <param-value>paraName2变量的值</param-value> </init-param> </filter> <filter-mapping> <filter-name>filterStudy</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>