Servlet filter 实现留言板留言过滤 实例源代码

    /**
    *@athor wjh
    **/
    一:前端留言板note.jsp

    <form action="checkLogin_Note" method="post" name="ly_form">
                    <div style="text-align: center;color:#a53d17;">
                        <label class="contact"><strong>留言标题:</strong></label>
                        <input type="text" name="ly_title"/>
                    </div>
                     <div style="text-align: center;color:#a53d17;margin-top:10px;">
                        <div>
                        <label class="contact"><strong>留言内容:</strong></label>
                        <textarea rows="5" cols="40" name="ly_content" class="contact_input"></textarea> 
                         </div>
                         <div id="face">
                            选择留言头像:<input type="radio" name="pic" value="images/face/pic1.gif" checked="checked" /><img src="images/face/pic1.gif" />
                                        <input type="radio" name="pic" value="images/face/pic2.gif" /><img src="images/face/pic2.gif" />
                                        <input type="radio" name="pic" value="images/face/pic3.gif" /><img src="images/face/pic3.gif" /><br />
                                        <input type="radio" name="pic" value="images/face/pic4.gif" /><img src="images/face/pic4.gif" />
                                        <input type="radio" name="pic" value="images/face/pic5.gif" /><img src="images/face/pic5.gif" />
                                        <input type="radio" name="pic" value="images/face/pic6.gif" /><img src="images/face/pic6.gif" />
                                        <input type="radio" name="pic" value="images/face/pic7.gif" /><img src="images/face/pic7.gif" /><br />
                                        <input type="radio" name="pic" value="images/face/pic8.gif" /><img src="images/face/pic8.gif" />
                         </div>
                         <div class="form_row">
                            <input type="submit" class="register" value="发表留言" />
                         </div>   
                    </div>
                </form>

二:设置过滤器类
public class WordsFilter implements Filter {

private Map<String, String>map = new HashMap<String, String>();
//设置过滤器的配置对象
public void init(FilterConfig config) throws ServletException {
    String filePath = config.getInitParameter("filePath");//从配置文件中取得文件的相对路径
    ServletContext context = config.getServletContext();
    String realPath = context.getRealPath(filePath);//根据相对路径取得绝对路径
    try {
        FileReader freader = new FileReader(realPath);//根据绝对路径,通过文件流来读取文件
        BufferedReader br = new BufferedReader(freader);
        String line = null;
        while((line=br.readLine()) != null) {
            String []str = line.split("=");
            map.put(str[0], str[1]);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

//设置过滤器字符编码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//乱码处理
request.setCharacterEncoding(“gb2312”);
response.setContentType(“text/html;charset=gb2312”);
HttpServletRequest HttpReq = (HttpServletRequest) request;
HttpReqWrapper hrw = new HttpReqWrapper(HttpReq);
hrw.setMap(map);
chain.doFilter(hrw, response);

}

public void destroy() {
    System.out.println("--过滤器的销毁--");
}

}
三:设置过滤器方法类

public class HttpReqWrapper extends HttpServletRequestWrapper {

private Map<String,String> map = null;

public HttpReqWrapper(HttpServletRequest request) {
    super(request);
}
//过滤脏话
public String replace(String str){
    StringBuffer sb = new StringBuffer(str);
    Set<String>keys = this.getMap().keySet();
    Iterator<String>it = keys.iterator();
    String ss = null;
    while(it.hasNext()) {
        String key = it.next();
        System.out.println("key="+key);
        int index = sb.indexOf(key);
        System.out.println("index="+index);
        if(index != -1) {
            if(key != null)
                ss = key;
            sb.replace(index, index+key.length(), this.getMap().get(key));
        }
    }
    System.out.println("ss="+ss);
    System.out.println("过滤后的content="+sb.toString());
    if(ss!=null) {
        if (sb.toString().indexOf(ss) == -1) {
            return sb.toString();
        } else {
            System.out.println("进来了!");
            return replace(sb.toString());
        }
    }
    return sb.toString();
}

// 重写getParameter()方法
public String getParameter(String str) {
System.out.println(“str=”+str);
if(str.equals(“pager.offset”)){
return super.getParameter(str);
}else{
String content = super.getParameter(str);
System.out.println(“还没过滤的content=”+content);
return replace(content);
}
}

public Map<String,String> getMap() {
    return map;
}

public void setMap(Map<String,String> map) {
    this.map = map;
}

}
四:配置wen.xml

<filter>
    <filter-name>WordsFilter</filter-name>
    <filter-class>com.lovo.cq.shopping10_1.filter.WordsFilter</filter-class>
    <init-param>
        <param-name>filePath</param-name>
        <param-value>/WEB-INF/word.txt</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>WordsFilter</filter-name>
    <url-pattern>/checkLogin_Note</url-pattern>
</filter-mapping>   

**
This is the description of my J2EE component
This is the display name of my J2EE component
CheckLogin_Note
com.lovo.cq.shopping10_1.servlet.CheckLogin_Note


CheckLogin_Note
/checkLogin_Note
**
五、留言板过滤字符设置在web-inf下就是word.txt
妈的=**
老子=**
狗日的=*
我日=我*
TMD=*
我靠=我*
贱人=**
滚=*
操=*

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值