<< [信管征文]2012年下半年一次过高项(个人攻略总结) - 综合知识 - 信管网 | 首页 | 还在找“p2psearcher”?看看所谓“种子搜索神器”是个什么东西! >>

java - How can I get the HTTP status code out of a ServletResponse in a ServletFilter? - Stack Overflow

First, you need to save the status code in an accessible place. The best to wrap the response with your implementation and keep it there:

public class StatusExposingServletResponse extends HttpServletResponseWrapper {       private int httpStatus;       public StatusExposingServletResponse(HttpServletResponse response) {          super(response);      }       @Override      public void sendError(int sc) throws IOException {          httpStatus = sc;          super.sendError(sc);      }       @Override      public void sendError(int sc, String msg) throws IOException {          httpStatus = sc;          super.sendError(sc, msg);      }        @Override      public void setStatus(int sc) {          httpStatus = sc;          super.setStatus(sc);      }       public int getStatus() {          return httpStatus;     }  }

In order to use this wrapper, you need to add a servlet filter, were you can do your reporting:

public class StatusReportingFilter implements Filter {       public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {          StatusExposingServletResponse response = new StatusExposingServletResponse((HttpServletResponse)res);          chain.doFilter(req, response);          int status = response.getStatus();         // report      }       public void init(FilterConfig config) throws ServletException {          //empty      }       public void destroy() {         // empty     }  }

阅读全文……




发表评论 发送引用通报