欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

敏感词过滤

程序员文章站 2022-07-12 18:57:56
...

<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>

过滤器应用示例---敏感词过滤

过滤器应用示例---敏感词过滤

用户名:

留言板 --------------------- package cn.hncu.pubs;

import java.util.ArrayList;
import java.util.List;

public class WordsUtil {
private static List list = new ArrayList();
static{
//这里应该从数据库中导入敏感词的,我在这里就直接用词来模拟了
list.add(“骂人”);
list.add(“sb”);
}
public static List getWords(){
return list;
}
public static void reBuild(){
//把list中的内容存储到数据库—每一段时间存储一次
}
}

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NoteServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.println("不支持GET方式留言");
    out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");

    String name = request.getParameter("name");
    if(name==null || name.trim().length()<=0){
        out.println("名字不能为空!!!");
    }else{
        String words = request.getParameter("note");
        out.println("<br/>"+name+"的留言是:<br/>"+ words);
    }
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
}

}

package cn.hncu.filter;

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.HttpServletRequestWrapper;

import cn.hncu.pubs.WordsUtil;

public class WordFilter implements Filter{

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding("utf-8");

    //在过滤器中用装饰模式把 原装request的功能增强了
    //---拦截后台调用的getParamter()方法
    MyRequest req = new MyRequest((HttpServletRequest)request);

    chain.doFilter(req, response);//放行
}

@Override
public void destroy() {
}

}

class MyRequest extends HttpServletRequestWrapper{
public MyRequest(HttpServletRequest request) {
super(request);
}

@Override
public String getParameter(String name) {
    String str = super.getParameter(name);
    List<String> list = WordsUtil.getWords();
    for(String word : list){
        str = str.replaceAll(word, "*");
    }
    return str;
}

}

<?xml version="1.0" encoding="UTF-8"?>




wordFilter
cn.hncu.filter.WordFilter


wordFilter
/*

NoteServlet cn.hncu.servlet.NoteServlet


NoteServlet
/NoteServlet


index.jsp