当前位置:网站首页>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);
}
完
边栏推荐
猜你喜欢
How to adjust Chinese in joiplay simulator
The difference between h264 and h265 decoding
Jmeter参数传递方式(token传递,接口关联等)
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
Strict Mode for Databases
Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
Linux 部署mysql 5.7全程跟踪 完整步骤 django部署
How to solve the error of joiplay simulator
h264和h265解码上的区别
消息队列存储消息数据的MySQL表设计
随机推荐
Regular expression password policy and regular backtracking mechanism bypass
A Brief Talk About MPI
Xss target drone training [success when pop-up window is realized]
xss的绕过
寄存器(汇编语言)
Point Cloud Scene Reconstruction with Depth Estimation
ELK部署脚本---亲测可用
Android security optimization - APP reinforcement
Common network status codes
what is jira
redis学习
transition transition && animation animation
How to solve types joiplay simulator does not support this game
Jetpack Compose学习(8)——State及remeber
Machine Learning 1-Regression Model (2)
【深入浅出玩转FPGA学习15----------时序分析基础】
【唐宇迪 深度学习-3D点云实战系列】学习笔记
45.【list链表的应用】
mysql主从复制及读写分离脚本-亲测可用
MySQL数据库(基础)