1.url-pattern:Filter的拦截路径,即浏览器在请求什么位置的资源时,过滤器会进行拦截
2.精准匹配<url-pattern>/a.jsp</url-pattern>对应的请求地址:http://ip[域名]:port/工程路径/a.jsp会拦截
3.目录匹配<url-pattern>/manage/*</url-pattern>对应的请求地址http://ip[域名]:port/工程路径/manage/xx,即web工程manage目录下所有的资源都会拦截
4.后缀名匹配
<!--
<url-pattern>*.jsp</url-pattern>后缀可改变,比如:*.action *.do等等对应的请求
-->
5.Filter过滤器只关心请求的地址是否匹配,不关心请求的资源是否存在
?
1.filter在web项目启动时,由tomcat来创建filter实例,只会创建一个
2.会调用filter的无参构造器,同时会调用init()方法,只会调用一次
3.在创建filter实例时,同时会创建FilterConfig对象,并通过init()方法传入
4.通过FilterConfig对象,可以获取该filter的相关配置信息
5.当一个http请求和该filter的url-patter匹配时,就会调用doFilter方法
6.在调用doFilter方法时,tomcat会同时创建 servletRequest对象 和 servletResponse对象 和 filterChain对象并通过doFilter方法传入
7.如果后面的请求目标资源(jsp/html...)会使用到request和response ,那么会继续传递
1.FilterConfig时Filter过滤器的配置类
2.Tomcat每次创建FIlter的时候,也会创建一个FilterConfig对象,这里包含了Filter配置文件的配置信息
3.FilterConfig对象作用是获取filter过滤器的配置内容
package com.filter;
import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
/**
* 演示FilterConfig的使用
*/
public class FilterConfigTest implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//通过filterConfig 获取相关的参数
String filterName = filterConfig.getFilterName();
String ip = filterConfig.getInitParameter("ip");
ServletContext servletContext = filterConfig.getServletContext();
//可以获取到该filter所有的配置参数名
Enumeration<String> initParameterNames = filterConfig.getInitParameterNames();
//遍历枚举
while (initParameterNames.hasMoreElements()){
System.out.println("名字="+initParameterNames.nextElement());
}
System.out.println("FilterName=" + filterName);
System.out.println("ip=" + ip);
System.out.println("servletContext=" + servletContext);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
@Override
public void destroy() {
}
}
<filter>
<filter-name>FilterConfigTest</filter-name>
<filter-class>com.filter.FilterConfigTest</filter-class>
<init-param>
<param-name>id</param-name>
<param-value>168.16.15.13</param-value>
</init-param>
<init-param>
<param-name>port</param-name>
<param-value>8989</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FilterConfigTest</filter-name>
<url-pattern>/abc/*</url-pattern>
</filter-mapping>
注意:web.xml文件中需要写一个IP,然后值为128.12,如下
<filter>
<filter-name>FilterConfigTest</filter-name>
<filter-class>com.filter.FilterConfigTest</filter-class>
<init-param>
<param-name>id</param-name>
<param-value>128.12</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FilterConfigTest</filter-name>
<url-pattern>/abc/*</url-pattern>
</filter-mapping>
public class FilterConfigTest implements Filter {
private String ip;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ip = filterConfig.getInitParameter("ip");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//通过forbidden ip来进行控制
//先获取到访问ip
String remoteAddr = servletRequest.getRemoteAddr();
if(remoteAddr.contains(ip)){
System.out.println("封杀该网段");
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
return;//直接返回
}
//继续访问目标资源
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}