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.HttpServletResponse; public class CompatibleFilter implements Filter { public Boolean isNeedCompliant = true; // 强制将浏览器文档模式转换为IE7 public String X_UA_Compatible = "IE=EmulateIE7"; public void init(FilterConfig filterConfig) throws ServletException { String compatible = filterConfig.getInitParameter("compatible"); String needCompliant = filterConfig.getInitParameter("isNeedCompliant"); if (compatible != null) X_UA_Compatible = compatible; if (needCompliant != null && needCompliant.equals("false")) isNeedCompliant = false; } public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException{ if(isNeedCompliant == true){ HttpServletResponse response = (HttpServletResponse) res; response.addHeader("X-UA-Compatible", X_UA_Compatible); chain.doFilter(req, response); }else{ chain.doFilter(req,res); } } public void destroy() { } }
转载于:https://www.cnblogs.com/weifeng0729/p/4226063.html