Filter——实现敏感字拦截

来源:互联网 发布:照片打分软件 编辑:程序博客网 时间:2024/05/01 16:12

小demo,实现评论区的敏感字拦截,就不连接数据库了,直接创建一个类,用于保存敏感性词汇,如下BadWords类

import java.util.ArrayList;import java.util.List;public class BadWords {   public static List<String> getBadWords(){      String w1="sb";      String w2="傻逼";      String w3="挂机";      String w4="送人头";      String w5="抢野";      String w6="叫爸爸";      String w7="你妈";      String w8="我去";       List<String> word=new ArrayList<String>();       word.add(w1);       word.add(w2);       word.add(w3);       word.add(w4);       word.add(w5);       word.add(w6);       word.add(w7);       word.add(w8);       return word;   }}

然后创建一个index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>留言板</title></head><body><form action="a" method="post"><textarea rows="20" cols="15" name="content">请输入留言...</textarea><input type="submit" value="发表"></form></body></html>

创建一个过滤器类

import java.io.IOException;import java.util.List;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.hk.utils.BadWords;/* * 实现敏感字拦截*/public class BadWordFilter implements Filter{    @Override    public void destroy() {    }    @Override    public void doFilter(ServletRequest req, ServletResponse resp,            FilterChain chain) throws IOException, ServletException {          HttpServletRequest request=(HttpServletRequest) req;          HttpServletResponse response=(HttpServletResponse) resp;          //获取请求中的内容          String str = req.getParameter("content");          //判断字符串中是否包含敏感词          List<String> list=BadWords.getBadWords();          if(str!=null){          for (String word : list) {            if(str.contains(word)){            String var=xin(word.length());            str=str.replace(word, var);            System.out.println(var);            System.out.println(str);            //request.setAttribute("content", content);            }          }          }          request.setAttribute("content", str);           chain.doFilter(request, response);    }    private String xin(int length) {        String xin="";        for (int i = 0; i < length; i++) {            xin+="*";        }        return xin;    }    @Override    public void init(FilterConfig arg0) throws ServletException {    }}

然后创建一个Servlet,用于显示过滤后的字段值

import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        doPost(req, resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {            PrintWriter out = resp.getWriter();            String content =(String) req.getAttribute("content");            out.println(content);            out.close();    }}

经过配置后部署后,启动服务器,在评论区打“对面五傻逼又挂机了”,点击提交后,会在页面显示对面五 **又**了
注意:该例同时也要配合乱码过滤器,且在配置时,乱码过滤器要在前面,不然会出现中文乱码问题

原创粉丝点击