HttpServletResponseWrapper 状态代码 = 0

发布于 2024-11-05 05:53:37 字数 268 浏览 7 评论 0原文

我正在使用 HttpServletResponseWrapper 捕获 servlet 过滤器中的状态代码。看起来效果很好。

当一切正常时,我的状态为 200。但是,当应用程序服务器找不到请求的项目时,我会返回 0。但在浏览器中它显示为 404。

有人可以解释一下吗?

编辑:这是一个 JAX-RS Web 应用程序,所以我猜测如果应用程序服务器在不设置状态的情况下无法匹配它返回的路径,那么当 Web 服务器看到状态 0 时,它会将其替换为 404。这听起来怎么样正确的?

I am using HttpServletResponseWrapper to capture the status code in a servlet filter. It seems to work fine.

I get a status of 200 when everything is ok. However when the app server cant find the requested item, I get back a 0. but in the browser it shows up as a 404.

Can someone explain this?

EDIT: This is a JAX-RS web app so I am guessing that if the app server can't match the path it returns without setting a status, then when the webserver see a status of 0 it replaces it with 404. Does this sound right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-11-12 05:53:37

我迟到了,但答案可能仍然有用:

在 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,您需要捕获那里的状态。

I am late but probably the answer is still useful:

In the HttpServletResponseWrapper these methods need to be implemented:

@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);
    }

On case of 404 the setStatus is not called but sendError, you need to catch the status there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文