注意:
1、过滤器慎用日志、中文乱码、敏感字符
2、web对过滤器、监听和servlet的调用顺序
先用监听-----过滤器—再调用servlet
敏感字符过滤的两种方法:
1、把敏感字符放入到一个文件,从文件中读取这些字符,与请求对象中字符进行比较,如果包含有铭感字符,一般用某种符号替换
2、把敏感字符放入到数据库中,从数据库的表中读取这些字符。
过滤器的作用:拦截request对象,将请求传递到request的包装类中
效果图:
首先创建一个txt文本,然后在filter过滤器中进行过滤。
提示:只放了重要代码,自动生成的代码就没有放入了。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest sp=(HttpServletRequest)request;
HttpServletResponse sResponse=(HttpServletResponse)response;
//请求的包装,过滤器的作用:拦截request对象,将请求传递到request的包装
sp.setCharacterEncoding("UTF-8");
noderequest no=new noderequest(sp);
chain.doFilter(no, response);
}
class noderequest extends HttpServletRequestWrapper{
HttpServletRequest request;
public noderequest(HttpServletRequest request) {
super(request);
this.request=request;
// TODO Auto-generated constructor stub
}
@Override
public String getParameter(String name) {
//获得留言内容
String nodestr=request.getParameter(name);
//获得敏感词库
List<String> lists=wordutil.getWords();
//进行比对
//取每一个
//当读取到敏感字符用*号代替
for(String str:lists) {
nodestr=nodestr.replaceAll(str, "*");
}
return nodestr;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
欧文晶
关注
0
0
0
javaWeb实现的过滤器敏感字过滤
03-17
javaWeb实现的过滤器敏感字过滤,建一个敏感字符的文件,发送时实现过滤为***
————————————————
版权声明:本文为CSDN博主「欧文晶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42017951/article/details/88564102