过滤器实现简单敏感词替换filter

敏感词替换:

package com.hanqi.web;

import java.io.IOException;
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;

/**
 * Servlet Filter implementation class HWmingan
 */
public class HWminganF implements Filter {

    /**
     * Default constructor. 
     */
    public HWminganF() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @return 
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        String s=request.getParameter("liuyan");
        s=s.replace("1", "*");
        request.setAttribute("huan", s);
        
        
        chain.doFilter(request, response);
        
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}
package com.hanqi.web;

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;

/**
 * Servlet implementation class HWminganS
 */
public class HWminganS extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HWminganS() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw=response.getWriter();
        String s=(String)request.getAttribute("huan");
        
        if(s!=null)
        {
            if(s!="")
            {
            pw.println(s);
            }
        }
        else
        {
            pw.println("内容不能为空");
        }
        
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
<%@ 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="s" method="post">
<table>
<tr><td>请输入留言内容:</td></tr>
<tr><td><textarea  name="liuyan" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="submit" value="提交"></td></tr>
</table>
</form>
</body>
</html>

运行结果:

posted @ 2016-07-04 10:20  鱼在我这里  阅读(3616)  评论(1编辑  收藏  举报