java过滤非法字符的filter
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java过滤非法字符的filter相关的知识,希望对你有一定的参考价值。
需求:
发现非法字符就替换成空,然后继续往下执行。
别发现非法字符就,跳到个页面说,你有非法字符,这样不行。
用javax.servlet.Filter实现。
好难啊,help。
二楼没仔细看需求啊,跳到error页面不算啊
jsp页面的编码你设成你自己的,我用的是utf-8。
input.jsp输入后,正常跳转到handle.jsp,而禁词已经被过滤。
filter:
package test;
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyFilter implements Filter
private List<String> unString;
public void init(FilterConfig filterConfig) throws ServletException
unString = new ArrayList<String>();
unString.add("日");
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
String content = request.getParameter("content");//需要过滤的参数
if(content!=null)
for (int i = 0; i < unString.size(); i++)
String strIllegal = unString.get(i);
if (content.indexOf(strIllegal) >= 0)
content = content.replaceAll(strIllegal, "");//非法字符替换成空
request.setAttribute("content", content);//为request设置属性保存修改后的值
chain.doFilter(request, response);
public void destroy()
//System.out.println("过滤器销毁");
//---------------------------//
web.xml:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>test.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//---------------------------//
输入页面input.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>input.jsp</title>
</head>
<body>
<form action="handle.jsp" method="post">
<input type="text" name="content" />
<input type="submit" value=" 提交 " />
</form>
</body>
</html>
//---------------------------//
input提交的页面handle.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> handle.jsp </title>
</head>
<body>
<%
String content = (String)request.getAttribute("content");
out.println(content);
%>
</body>
</html> 参考技术A import java.io.*;
import javax.servlet.*;
public class CharFilter implements Filter
private List<String> unString;
public void init(FilterConfig filterConfig)throws ServletException
unString=new ArrayList<String>();
unString.add("日");
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
String content=request.getParameter("content");//需要过滤的参数
for(int i=0;i<unString.size();i++)
if(content.indexOf(unString.get(i)>=0) System.out.println("有非法文字");
request.getRequestDispatcher("error.jsp").forward(request,response);
chain.doFilter(request,response);
public void destroy()
//System.out.println("过滤器销毁");
-----------------
web.xml
----------------
<filter>
<filter-name> char </filter-name>
<filter-class> cn.mldn.lxh.filter.CharFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> char </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping> 参考技术B 你用的是什么框架,FILTER程序是自己写的吗?
代码贴过来看看
非法字符空字符的过滤
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
* Description:字符串处理工具类
* </p>
*
* @author dailei
* @version V1.0.0
* @className StringUtils
* @date 2017/5/23
*/
public class StringUtils
/**
* 如果数据异常,去掉空,回车,tab的异常字符
* @param str 需要转化的字符串
* @return 目标字符串
*/
public static String replaceBlank(String str)
String dest = "";
if (str != null)
Pattern p = Pattern.compile("\\\\s*|\\t|\\r|\\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
return dest;
以上是关于java过滤非法字符的filter的主要内容,如果未能解决你的问题,请参考以下文章