简单使用jstl实现敏感字替换

Posted Claricre

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单使用jstl实现敏感字替换相关的知识,希望对你有一定的参考价值。

package com.ceshi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

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 javax.servlet.http.HttpServletResponse;

public class CharFilter implements Filter{
    private ArrayList<String> list = new ArrayList<String>();
    @Override
    public void destroy() {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse rep=(HttpServletResponse)response;
        MyRequest mreq = new MyRequest(req);
        
        chain.doFilter(mreq, rep);
    }

    @Override
    public void init(FilterConfig cfg) throws ServletException {
        String val=cfg.getInitParameter("dirtywords");
        String[] ss=val.split(",");
        list.addAll(Arrays.asList(ss));
        
    }
    class MyRequest extends HttpServletRequestWrapper{
        private HttpServletRequest request;
        public MyRequest(HttpServletRequest request) {
            super(request);
            this.request=request;
            
        }
        @Override
        public String getParameter(String name) {
            String txt=this.request.getParameter("txt");
            for(String s:list){
                txt=txt.replaceAll(s, "**");
            }
            return txt;
        }
        
    }

}

 

package com.ceshi;

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;
import javax.servlet.http.HttpServletResponse;

public class TestFilter implements Filter{

    @Override
    public void destroy() {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // TODO 自动生成的方法存根
        //HttpServletRequest req=(HttpServletRequest)request;
        //HttpServletResponse rep = (HttpServletResponse)response;
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //response.getWriter().write("kaishi");
        chain.doFilter(request, response);
        //response.getWriter().write("jieshu");
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO 自动生成的方法存根
        
    }

}

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

     <filter>
        <filter-name>testfilter</filter-name>
        <filter-class>com.ceshi.TestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>testfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <filter>
        <filter-name>CharFilter</filter-name>
        <filter-class>com.ceshi.CharFilter</filter-class>
        <init-param>
            <param-name>dirtywords</param-name>
            <param-value>tmd,cnm,nmb</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    
  <display-name>Test0213pm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

NewFile.jsp界面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="txt.jsp" method="post">
<textarea rows="10" cols="20" name="txt">
asdasdadasdtmdnmbasdasdnmb
</textarea>
<input type="submit" value="tijiao"/>
</form>
</body>
</html>

 

txt.jsp界面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${param.txt }
</body>
</html>

实现效果如下:

 

 

以上是关于简单使用jstl实现敏感字替换的主要内容,如果未能解决你的问题,请参考以下文章

修改敏感字

jstl标签库基础教程及其使用代码

Java 测试:将用户输入的字符串中的敏感字和谐成星星

DFA算法(敏感字屏蔽)

DFA算法(敏感字屏蔽)

django+SQLite搭建轻量级个人博客前端页面敏感词替换