浏览器不缓存页面的过滤器
System.out.println("cacheFilterDO"); response.setDateHeader("Expires", -1); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); chain.doFilter(request, response);
解决中文乱码的过滤器Filter
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.sdut.util.cn.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
public void init(FilterConfig filterConfig) throws ServletException { encoding = filterConfig.getInitParameter("encoding"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(encoding);//解决中文问题 chain.doFilter(request, response); }
应用二(过滤论坛不雅字符例如shit,fuck等)
1,因为每次书写***Filter类都要继承Filter比较繁杂因此书写HttpFilter类
每次都 extends HttpFilter代码简便
HttpFilter.java
package com.wlc.DuoduiYi.servlet; import java.io.IOException; 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.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 自定义的httpfilter实现filter接口 * * @author wlc * */ public abstract class HttpFilter implements Filter { public void destroy() { } /** * 原生的dofilter方法将方法内部的ServletRequest , ServletResponse转为 HttpServletRequest * ,HttpServletResponse 并调用了 doFilter(HttpServletRequest request, * HttpServletResponse response, FilterChain chain)方法 * 若编写filter的过滤方法不建议继承该方法而应该继承doFilter(HttpServletRequest request, * HttpServletResponse response, FilterChain chain)方法 */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; doFilter(request, response, chain); } /** * 抽象方法为http请求定制 必须实现的方法 * * @param request * @param response * @param chain * @throws IOException * @throws ServletException */ // abstract定义抽象类或者抽象方法;抽象方法是指只定义了一个方法,但没有实现方法体; public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException; /** * yon用于保存filterconfig对象 */ private FilterConfig filterConfig; /** * 不建议子类直接覆盖若直接覆盖将会导致filterConfig成员变量初始化失败 */ public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; init(); } /** * 供子类继承的初始化方法可以通过getfilterConfig()获取FilterConfig对象 */ protected void init() { } /** * 直接返回init(servletConfig)的filterConfig对象 * * @return */ public FilterConfig getFilterConfig() { return filterConfig; } }
BBSFilter.java
package com.wlc.DuoduiYi.servlet; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BBSFilter extends HttpFilter { @Override public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = new MyHttpServletRequest(request); System.out.println("我是BBSFilter"); chain.doFilter(req, response); System.out.println("我是BBSFilter"); } }
MyHttpServletResqust.java
package com.wlc.DuoduiYi.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class MyHttpServletRequest extends HttpServletRequestWrapper { public MyHttpServletRequest(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { String val = super.getParameter(name); if (val != null && val.contains(" fuck ")) { val = val.replace("fuck", "***"); System.out.println("222"); } return val; } }
FontFilter.jsp
<body> <form action="helloNew.jsp" method="post"> <table border="1"> <tr> <td>BBS:</td> <td><input type="text" name="content" /></td> </tr> <tr> <td><input type="submit" value="submit" /> </td> </tr> </table> </form> </body>
helloNew.jsp
<body> HELLO:${param.content} <br> <br> </body>
输出结果