日期:2014-05-17 浏览次数:20755 次
/** *分享自己写的一个request包装类,解决get方式提交数据的乱码 *html标签转义、关键词过滤问题 */
package com.hcy.test.filter; import java.io.UnsupportedEncodingException; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class MyHttpRequest extends HttpServletRequestWrapper { HttpServletRequest request; String[] dWords={"fuck","shit","操"}; public MyHttpRequest(HttpServletRequest request) { super(request); this.request=request; } @Override public String getParameter(String name) { String value=request.getParameter(name); try { //对所有的参数重新编码 value=new String(value.getBytes("ISO-8859-1"),"UTF-8"); //对html标签进行转义 value= filter(value); //对某些词汇进行过滤 for(String dw:dWords){ value=value.replace(dw, "**"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return value; } public String filter(String message) { if (message == null) return (null); char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return (result.toString()); } }