当前位置:网站首页>Filter (Filter)
Filter (Filter)
2022-07-31 00:23:00 【^O^——】
目录
四、Get the initialization parameters of the filter configuration
一、如何使用过滤器(Filter)
1.1.写一个类实现(implements)Filter(过滤器类,Filter来自 Filter - javax,servlet接口中).
1.2.重写三个方法
@Override
public void destroy() {
System.out.println("过滤器被销毁!");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("The request was intercepted by the filter!");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// 启动Tomcat服务
System.out.println("过滤器初始化/过滤器被创建!");
}二、通过xml配置过滤器
<!-- 配置过滤器 -->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.zking.filter.LoginFilter</filter-class>
<!-- Configure filter initialization parameters -->
<!-- 参数1 -->
<init-param>
<!-- 参数名 -->
<param-name>name</param-name>
<!-- 参数值 -->
<param-value>张三</param-value>
</init-param>
<!-- 参数2 -->
<init-param>
<!-- 参数名 -->
<param-name>sex</param-name>
<!-- 参数值 -->
<param-value>男</param-value>
</init-param>
</filter>
<!-- Configure the filter map file -->
<filter-mapping>
<filter-name>loginFilter</filter-name>
<!-- Set the request to filter by the filter -->
<!-- 1.filter a single request -->
<url-pattern>/index.jsp</url-pattern>
<!-- 2.Filter requests with the specified suffix -->
<!-- 如:Filter all suffix names.jsp的页面 -->
<url-pattern>*.jsp</url-pattern>
<!-- 3.Filter requests for the specified directory -->
<url-pattern>/home/index.jsp</url-pattern>
<!-- 4.Filter all requests for the entire project -->
<url-pattern>/*</url-pattern>
</filter-mapping>
三、通过注解配置过滤器
3.1.Configure to filter individual requests
@WebFilter("/index.jsp")3.2.Configure to filter multiple requests
@WebFilter(
filterName = "loginFilter",// Configure the filter name,相当于<filter-name>loginFilter</filter-name>
urlPatterns = {"/login.jsp","/index.jsp"},// Configure to filter multiple pages
// 配置初始化参数
initParams = {
@WebInitParam(name = "name",value = "张三"),
@WebInitParam(name = "sex",value = "男")
}
)
四、Get the initialization parameters of the filter configuration
// Get the initialization parameters of the configuration in the initialization method
@Override
public void init(FilterConfig arg0) throws ServletException {
// 启动Tomcat服务
System.out.println("过滤器初始化/过滤器被创建!");
// 获取配置的初始化参数
String name = arg0.getInitParameter("name");
String sex = arg0.getInitParameter("sex");
System.out.println("name = " + name);
System.out.println("sex = " + sex);
}
五、过滤器的使用
1.转换参数arg0和arg1(要使用request和response里面的方法)
HttpServletRequest req = (HttpServletRequest)arg0;
HttpServletResponse resp = (HttpServletResponse)arg1;
2.设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("html/text;charset=utf-8");
3.使用arg2.doFilter(arg0, arg1),The page blocked by the filter can continue to function normally.
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("The request was intercepted by the filter!");
// 转换参数arg0和arg1
HttpServletRequest req = (HttpServletRequest)arg0;
HttpServletResponse resp = (HttpServletResponse)arg1;
// 设置编码
resp.setContentType("html/text;charset=utf-8");
// 放行/继续前进
arg2.doFilter(arg0, arg1);
}完
边栏推荐
猜你喜欢
随机推荐
joiplay模拟器如何导入游戏存档
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
45.【list链表的应用】
MySQL table design for message queue to store message data
PHP图片添加文字水印
46.
Point Cloud Scene Reconstruction with Depth Estimation
xss靶机训练【实现弹窗即成功】
mysql索引失效的常见9种原因详解
分布式系统的一致性与共识(1)-综述
MySQL数据库(基础)
一款好用的接口测试工具——Postman
Installation considerations for pytorch
what is jira
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
寄存器(汇编语言)
joiplay模拟器如何使用
MySQL笔记下
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
【愚公系列】2022年07月 Go教学课程 019-循环结构之for








![[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]](/img/1c/a88ba3b01d2e2302c26ed5f730b956.png)
