当前位置:网站首页>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);
}
完
边栏推荐
- MySQL系列一:账号管理与引擎
- Summary of the stock problem of state machine dynamic programming
- MySQL数据库(基础)
- Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
- 【多线程】
- How to use joiplay emulator
- After writing business code for many years, I found these 11 doorways, which only experts know
- MPI简谈
- 【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
- 限制字符绕过
猜你喜欢
随机推荐
joiplay模拟器rtp如何安装
Homework: iptables prevent nmap scan and binlog
牛客网刷题训练(四)
An easy-to-use interface testing tools - the Postman
Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
Android security optimization - APP reinforcement
Basic usage of async functions and await expressions in ES6
Point Cloud Scene Reconstruction with Depth Estimation
【深入浅出玩转FPGA学习15----------时序分析基础】
网络常用的状态码
MySQL grant statements
DNS resolution process [visit website]
会议OA项目待开会议、所有会议功能
Shell script if statement
joiplay模拟器如何调中文
从两个易错的笔试题深入理解自增运算符
2D转换模块&&媒体查询
.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
mysql主从复制及读写分离脚本-亲测可用
Optimization of aggregate mentioned at DATA AI Summit 2022