首先,列出我用到的参考资料吧:
资料一来自:http://magicgod.javaeye.com/blog/146537
资料二来自:http://blog.sina.com.cn/s/blog_56000153010005iq.html
我就是根据上面的两个资料,写了一个FILTER和一个JAVA 类,具体如下:
FILTER: SpecialCharacterFilter
view plaincopy to clipboardprint?
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class SpecialCharacterFilter extends HttpServlet implements Filter {
protected FilterConfig config;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
System.out.println("[SpecialCharacterFilter] : doFilter *************");
HttpServletRequest request = (HttpServletRequest)req;
HashMap<String, String[]> m = new HashMap(request.getParameterMap());
HashMap<String, String[]> mm = new HashMap<String, String[]>();
Enumeration<String> enu = request.getParameterNames();
if(m.size() > 0 && processParameters(m, enu, mm)) {
ParameterRequestWrapper wrapRequest = new ParameterRequestWrapper(request, mm);
chain.doFilter(wrapRequest, res);
} else {
chain.doFilter(req, res);
}
}
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
}
public boolean processParameters(HashMap<String, String[]> m, Enumeration enu, HashMap<String, String[]> mm) {
System.out.println("[SpecialCharacterFilter] : processParameters *************");
if (m != null && enu != null) {
while (enu.hasMoreElements()) {
String key = (String)enu.nextElement();
String [] values = (String [])m.get(key);
for(int i = 0; i < values.length; i ++) {
if (values[i] != null) {
values[i] = values[i].trim();
values[i] = values[i].replaceAll("&", "&");
values[i] = values[i].replaceAll("<", "<");
values[i] = values[i].replaceAll(">", ">");
values[i] = values[i].replaceAll("\t", " ");
values[i] = values[i].replaceAll("\r\n", "\n");
values[i] = values[i].replaceAll("\n", "<br>");
values[i] = values[i].replaceAll(" ", " ");
values[i] = values[i].replaceAll("'", "'");
values[i] = values[i].replaceAll("\\\\", "\");
}
}
mm.put(key, values);
}
}
return true;
}
}
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class SpecialCharacterFilter extends HttpServlet implements Filter {
protected FilterConfig config;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
System.out.println("[SpecialCharacterFilter] : doFilter *************");
HttpServletRequest request = (HttpServletRequest)req;
HashMap<String, String[]> m = new HashMap(request.getParameterMap());
HashMap<String, String[]> mm = new HashMap<String, String[]>();
Enumeration<String> enu = request.getParameterNames();
if(m.size() > 0 && processParameters(m, enu, mm)) {
ParameterRequestWrapper wrapRequest = new ParameterRequestWrapper(request, mm);
chain.doFilter(wrapRequest, res);
} else {
chain.doFilter(req, res);
}
}
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
}
public boolean processParameters(HashMap<String, String[]> m, Enumeration enu, HashMap<String, String[]> mm) {
System.out.println("[SpecialCharacterFilter] : processParameters *************");
if (m != null && enu != null) {
while (enu.hasMoreElements()) {
String key = (String)enu.nextElement();
String [] values = (String [])m.get(key);
for(int i = 0; i < values.length; i ++) {
if (values[i] != null) {
values[i] = values[i].trim();
values[i] = values[i].replaceAll("&", "&");
values[i] = values[i].replaceAll("<", "<");
values[i] = values[i].replaceAll(">", ">");
values[i] = values[i].replaceAll("\t", " ");
values[i] = values[i].replaceAll("\r\n", "\n");
values[i] = values[i].replaceAll("\n", "<br>");
values[i] = values[i].replaceAll(" ", " ");
values[i] = values[i].replaceAll("'", "'");
values[i] = values[i].replaceAll("\\\\", "\");
}
}
mm.put(key, values);
}
}
return true;
}
}
view plaincopy to clipboardprint?
JAVA 类:ParameterRequestWrapper
JAVA 类:ParameterRequestWrapper view plaincopy to clipboardprint?
<PRE class=java name="code">import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map params;
public ParameterRequestWrapper(HttpServletRequest request, Map newParams) {
super(request);
this.params = newParams;
}
public Map getParameterMap() {
return params;
}
public Enumeration getParameterNames() {
Vector l = new Vector(params.keySet());
return l.elements();
}
public String[] getParameterValues(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
return (String[]) v;
} else if (v instanceof String) {
return new String[] { (String) v };
} else {
return new String[] { v.toString() };
}
}
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else if (v instanceof String) {
return (String) v;
} else {
return v.toString();
}
}
}
</PRE>
<PRE class=java name="code">最后,还是写一下WEB.XML的配置吧:</PRE>
<PRE class=java name="code"><PRE class=xml name="code"> <filter>
<filter-name>specialCharacterFilter</filter-name>
<filter-class>
com.fhn.vismp.common.SpecialCharacterFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>specialCharacterFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>specialCharacterFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping></PRE>
</PRE>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wanchengpeng/archive/2008/07/09/2628926.aspx
posted on 2010-01-31 13:37
fly 阅读(4315)
评论(0) 编辑 收藏 所属分类:
jsp学习