2_过滤器的定义和配置
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
采用三步走策略使用filter
1开发后台资源 静态资源(html,css … …)或者动态资源(Servlet,Jsp)
2开发Filter
3在web.xml中配置Filter拦截哪些资源
开发Servlet
定义两个servlet
package com.msb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
public class MyServlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("myServlet1 执行了sevice方法");
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().print("myServlet1响应的数据");
}
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
package com.msb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
public class MyServlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("myServlet2 执行了sevice方法");
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().print("myServlet2响应的数据");
}
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
开发Filter
package com.msb.filter;
import javax.servlet.*;
import java.io.IOException;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
public class MyFilter implements Filter {
// 初始化方法
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
// 作出过滤的方法
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter doFilter 对请求作出过滤");
// 通过一行代码 放行请求
// 放行请求,交给过滤器链继续进行过滤 最后到达资源
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("Filter doFilter 对响应作出过滤");
servletResponse.getWriter().print("filter 追加一些数据");
}
// 销毁方法
@Override
public void destroy() {
}
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
配置Filter
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>myServlet1</servlet-name>
<servlet-class>com.msb.servlet.MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet1</servlet-name>
<url-pattern>/myServlet1.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>myServlet2</servlet-name>
<servlet-class>com.msb.servlet.MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet2</servlet-name>
<url-pattern>/myServlet2.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>filter1</filter-name>
<filter-class>com.msb.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<!--对那个/些资源的请求和响应进行过滤-->
<!--<url-pattern>/myServlet1.do</url-pattern>-->
<servlet-name>myServlet1</servlet-name>
<servlet-name>myServlet2</servlet-name>
<!--<url-pattern>/</url-pattern>
<url-pattern>/*</url-pattern>-->
</filter-mapping>
</web-app>
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230308150042684-584343144.gif)