1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package cn.iduoka.util.format;
import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter;
@WebFilter(filterName = "FormatFilter", urlPatterns = ("/")) public class FormatFilter implements Filter {
@Override public void destroy() {
}
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ResponseWrapper wrapper = new ResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
String result = wrapper.getResult();
result = result.replace("</html>", "<div style='display: none;'>hello world</div>\n</html>");
response.setContentLength(-1);
PrintWriter out = response.getWriter(); out.write(result); out.flush(); out.close(); }
@Override public void init(FilterConfig filterConfig) throws ServletException {
}
}
|