我使用HttpServletResponseWrapper捕获servlet筛选器中的状态代码。它似乎工作得很好。
当一切正常时,我得到的状态是200。但是,当应用程序服务器找不到请求的项目时,我会返回0。但在浏览器中它显示为404。
有人能解释一下吗?
编辑:这是一个JAX-RS web应用程序,所以我猜如果应用程序服务器不能匹配它返回的路径而不设置状态,那么当web服务器看到状态为0时,它会将其替换为404。这听起来对吗?
发布于 2017-10-11 22:46:27
我迟到了,但答案可能仍然有用:
在HttpServletResponseWrapper中,需要实现以下方法:
@Override
public void setStatus(int status) {
super.setStatus(status);
this.status = status;
}
@Override
public void sendError(int status) throws IOException {
this.status = status;
super.sendError(status);
}
@Override
public void sendError(int status, String msg) throws IOException {
this.status = status;
super.sendError(status, msg);
}
@Override
public void sendRedirect(String location) throws IOException {
this.status = 302;
super.sendRedirect(location);
}在404的情况下,不调用setStatus,而是调用sendError,您需要捕获那里的状态。
https://stackoverflow.com/questions/5886925
复制相似问题