@WebServlet("/FilterWordServlet") public class FilterWordServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置请求和编码格式 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //自己创建的请求方法,继承与原来的,改写了getParameter方法让其有过滤的业务 WordFilter wFilter=new WordFilter(request); String string=wFilter.getParameter("num"); System.out.println("---------------"); //out的响应方法。输出在页面上,让ajax得到这个去处理一些业务 PrintWriter out=response.getWriter(); out.println(string); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
我自己建了一个list集合的过滤词汇,以后修改可以从这里开始词汇的搜索算法,先建一个词汇类
public class Words { //过滤词汇的词典 static List<String> list=new ArrayList<>(); static{ list.add("你妹的"); list.add("sb"); list.add("滚"); } public static List<String> getList() { return list; } public static void setList(List<String> list) { Words.list = list; } }
这只是最基础的一种。高级的需要算法的实现,可以思考一下这个DFA算法,我感觉很不错