标签: 杂谈 |
1.先实现一个对Response的包装器:
- public
class StatusExposingServletRes ponse extends HttpServletResponseWrapp er { -
-
private int httpStatus; -
-
public StatusExposingServletRes ponse(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; -
} -
- }
- public
class StatusReportingFilter implements Filter { -
-
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { -
StatusExposingServletRes ponse response = new StatusExposingServletRes ponse((HttpServletResponse)res); -
chain.doFilter(req, response); -
int status = response.getStatus(); -
// report -
} -
-
public void init(FilterConfig config) throws ServletException { -
//empty -
} -
-
public void destroy() { -
// empty -
} -
- }