java过滤非法字符的filter
发布网友
发布时间:2022-05-01 13:09
我来回答
共3个回答
热心网友
时间:2023-10-14 04:53
filter代码在pujia12345提供的代码上改的;
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>
热心网友
时间:2023-10-14 04:53
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>
热心网友
时间:2023-10-14 04:54
你用的是什么框架,FILTER程序是自己写的吗?
代码贴过来看看
热心网友
时间:2023-10-14 04:53
filter代码在pujia12345提供的代码上改的;
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>
热心网友
时间:2023-10-14 04:53
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>
热心网友
时间:2023-10-14 04:54
你用的是什么框架,FILTER程序是自己写的吗?
代码贴过来看看
热心网友
时间:2023-10-14 04:53
filter代码在pujia12345提供的代码上改的;
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>
热心网友
时间:2023-10-14 04:53
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>
热心网友
时间:2023-10-14 04:54
你用的是什么框架,FILTER程序是自己写的吗?
代码贴过来看看