实现原理
普通http请求
- MyServletResponseWrapper重写HttpServletResponseWrapper的getWriter()返回MyPrintWriter
- MyPrintWriter重写PrintWriter的所有write 方法。
- MyPrintWriter定义一个容器接收write方法写入的字符
webservice请求
- MyServletResponseWrapper重写HttpServletResponseWrapper的getOutputStream()返回MyOutputStream
- MyOutputStream重写ServletOutputStream的write(int b)方法
- MyOutputStream定义一个容器接收write方法写入的字节
实现代码
@WebFilter(filterName="myFilter ",urlPatterns="/*")
public class MyFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)arg0;
HttpServletResponse resp = (HttpServletResponse)arg1;
MyServletResponseWrapper myresp = new MyServletResponseWrapper(resp);
arg2.doFilter(arg0, arg1);
System.out.println(myresp.getContent());
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
public class MyServletResponseWrapper extends HttpServletResponseWrapper implements ResponseContent{
private MyPrintWriter myPrintWriter