赞
踩
public class TestFilter implements Filter { public void init(FilterConfig arg0) throws ServletException { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { } }
要在web.xml中配置
- <filter>
-
- <filter-name>TestFilter</filter-name>
-
- <filter-class>com.wh.filter. TestFilter </filter-class>
- <init-param>
- <param-name>characterEncoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
-
-
-
- <filter-mapping>
- <filter-name> TestFilter </filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
自定义的filter,要实现Filter
其生命周期为init ,dofilter, destroy。
这些都基础的废话。
说说标题,最近要做处理一下request对象里面的内容,所以涉及到了filter。说起处理request,就得提
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper { public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) { super(servletRequest); } public String[] getParameterValues(String parameter) { return encodedValues; } public String getParameter(String parameter) { return cleanXSS(value); } public String getHeader(String name) { return cleanXSS(value); } private String cleanXSS(String value) { 这里对value进行一些处理。 return value; } }
在自定义的filter中,调用了这个类,发现走了但是没起效果。郁闷了好几天,最后才发现,
- req= new XssHttpServletRequestWrapper(req);
- filterChain.doFilter(req, response);
- return;
缺了这个:
filterChain.doFilter(req, response);
前面调用 new XssHttpServletRequestWrapper(req);仅仅是对request进行了处理,但处理了得保存呀,这个就类似于保存的功能。加上了果然起作用了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。