java过滤特殊字符的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java过滤特殊字符的问题相关的知识,希望对你有一定的参考价值。

在jsp页面中有一个字符串“A++”提交到action后我输出来变成了“A ”可能是把++当特殊字符处理了,我要在action里输出“A++”应该怎么处理呢

试试这个

public class CharacterEncodingFilter implements Filter

private Log log = LogFactory.getLog(CharacterEncodingFilter.class);

protected String encoding = "UTF-8";
protected String inputEncoding = "UTF-8";
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

public void destroy()
this.encoding = null;
this.inputEncoding = null;
this.filterConfig = null;


class Request extends HttpServletRequestWrapper

public Request(HttpServletRequest request)
super(request);


public String encoding(String input)
try
CharacterEncodingFilter.this.log.info(input);
byte[] bytes = input.getBytes(CharacterEncodingFilter.this.inputEncoding);
return new String(bytes, CharacterEncodingFilter.this.encoding);
catch (Exception ex)

return null;


private HttpServletRequest getHttpServletRequest()
return (HttpServletRequest) super.getRequest();


public String getParameter(String name)
return this.encoding(this.getHttpServletRequest().getParameter(name));


public String[] getParameterValues(String name)
String values[] = this.getHttpServletRequest().getParameterValues(name);
if (values != null)
for (int i = 0; i < values.length; i++)
values[i] = this.encoding(values[i]);


return values;



public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
if (this.ignore || (request.getCharacterEncoding() == null))
String encoding = this.selectEncoding(request);
if (encoding != null)
request = new Request((HttpServletRequest) request);
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/json;charset=" + encoding);


chain.doFilter(request, response);


public void init(FilterConfig filterConfig) throws ServletException
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
this.inputEncoding = filterConfig.getInitParameter("inputEncoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;



protected String selectEncoding(ServletRequest request)
return (this.encoding);



web.xml

<filter>
<filter-name>CharacterEncodingFilte</filter-name>
<filter-class>cn.qbq.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>inputEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilte</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
参考技术A 加一个过滤器程序吧。
package com.xunyehulian.struts ;
import javax.servlet.* ;
import javax.servlet.http.*;
import java.io.* ;
public class EncodingFilter implements Filter
public void init( FilterConfig config )



public void doFilter( ServletRequest request ,
ServletResponse response ,
FilterChain chain )
throws ServletException , IOException

HttpServletRequest req = (HttpServletRequest) request ;
HttpServletResponse res = (HttpServletResponse ) response ;
req.setCharacterEncoding( "GBK" ) ;
res.setCharacterEncoding( "GBK" ) ;
chain.doFilter( req , res ) ;

public void destroy()


然后在WEB-INF文件夹下的web.xml里配一下
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.xunyehulian.struts.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
参考技术B "+"在URL中会被当作空格处理。
必须使用URLEncoder将其变成URL编码。
或者使用 javascript 的 encodeURIComponent(url) 函数对URL进行编码转换。本回答被提问者采纳
参考技术C 写个代码过滤器就OK了 参考技术D 二楼给出的像是字符编码的过滤器
求教类似于这个问题的过滤器该怎么写?

java用正则表达式判定特殊字符是不是,存在就替换的问题?

我在String ss = "aabbcc[dd]eeff[gg]hhiijjkkll[mmnn]oopp";这个字符串,用正则表达式把[]这样的特殊字符过滤出来,就是把[dd]替换成dd,原来的字符串里面也变成aabbccddeeff...

String的replaceAll方法可以使用正则表达式进行搜索并替换,但不改变原字符串,如果需要改变原字符串,则需要用赋值语句:
ss=ss.replaceAll("正则表达式","替换内容");
比如将"aabbcc[dd]eeff[gg]hhiijjkkll[mmnn]oopp"里的[等特殊符号去掉:
ss=ss.replaceAll("\W","");
参考技术A 至少有两种正则表达式的方法:

1、直接调用String类的replaceAll(String regex, String replacement)方法

代码:
String ss = "aabbcc[dd]eeff[gg]hhiijjkkll[mmnn]oopp";
ss.replaceAll("[\\[\\]\\\\]", "");//返回结果,可打印或返回
优点:简单
缺点:不具有通用性,需求变动则不适用,例如[dd]替换成DD

2、通过匹配规则来截取字符串

代码:
String ss = "aabbcc[dd]eeff[gg]hhiijjkkll[mmnn]oopp";
//正则表达式,表示由非[、]、、的任意字符构成
String tmp = "[^\\[^\\^\\]^\\.]";
//正则表达式,由5组构成第一组是普通字符串,第二组是"["字符串,第三组是普通字符串,第四组是"]"字符串,第五组是普通字符串
Pattern pattern = Pattern.compile("(" + tmp + "*)(\\[\\)(" + tmp + "*)(\\\\])(" + tmp + "*)");
Matcher matcher = pattern.matcher(ss);
StringBuffer result = new StringBuffer();
while(matcher.find())
result.append(matcher.group(1) + matcher.group(3) + matcher.group(5));

//result就是最终结果。

优点:具有一定的通用性,可自己定制需要的字符串,新字符串可以由原字符串的部分组成,也可与其他字符拼装,能更好满足可重用性,例如需求变更成[dd]替换成DD,代码只需将result.append(matcher.group(1)+matcher.group(3)+matcher.group(5));换成result.append(matcher.group(1)+matcher.group(3).toUpperCase()+matcher.group(5));就能完成

缺点:代码较长

PS:StringBuffer可转化成String再返回
参考技术B [\[\\\]]+

在java中

String ss = ss.replaceAll("[\\[\\\\\\]]+", "");

以上是关于java过滤特殊字符的问题的主要内容,如果未能解决你的问题,请参考以下文章

php怎样过滤掉特殊字符啊 ☺

java正则表达式过滤特殊字符

PHP怎样过滤中文状态下特殊字符(比如标点符号)?

Java用正则表达式 过滤特殊字符,只允许中文、字母或数字,该怎么写?急。。。。

如何用js或则jquery过滤特殊字符

jsp\java如何编写过滤器过滤特殊字符