我正在使用HttpServletResponseWrapper来捕获servlet过滤器中的状态代码 . 它似乎工作正常 .
一切正常,我的状态为200 . 但是,当应用服务器无法找到所请求的项目时,我会返回0.但在浏览器中它显示为404 .
有人可以解释一下吗?
编辑:这是一个JAX-RS网络应用程序,所以我猜测如果应用服务器无法匹配它返回的路径而不设置状态,那么当网络服务器看到状态为0时,它将用404替换它 . 这听起来是否有声音对?
我迟到但可能答案仍然有用:
在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,您需要在那里捕获状态 .
1 回答
我迟到但可能答案仍然有用:
在HttpServletResponseWrapper中,需要实现这些方法:
在404的情况下,不调用setStatus而是调用sendError,您需要在那里捕获状态 .