HttpServletResponse.sendError()不会重定向到错误页面 [英] HttpServletResponse.sendError() does not redirect to error page

查看:2093
本文介绍了HttpServletResponse.sendError()不会重定向到错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在doFilter()方法中有以下代码,我获取应用程序作用域bean。

I have the following code in doFilter() method, where I get application scoped bean.

    if (request.getServletContext().getAttribute("resource")==null) {
        response.sendError(503);
        return;
    }

我将503代码映射到web.xml中的特定错误页面。如果发生错误,我真的会在浏览器中获得错误页面的内容。但是地址栏中的地址不会因错误页面地址而改变:请求的servlet的地址离开那里。这是正确的行为吗?我想明确告知重定向到错误页面。
是否只能使用sendRedirect()?

I mapped 503 code to specific error page in web.xml. And I really get content of error page in browser, if error occurs. But the address in address bar doesn't change for error page address: an address of requested servlet leaves there. Is it right behaviour? I'd like to inform explicitly about redirection to error page. Is it only possible with sendRedirect()?

推荐答案

这是正确的行为。当您使用sendError()时,它将使用错误页面响应当前请求。如果您希望将URL更改为错误页面URL,则需要使用sendRedirect()来响应重定向。

This is the correct behavior. When you use sendError() it will respond to the current request with an error page. If you instead want the URL to change to the error page URL, you will need to use sendRedirect() to respond with a redirect.

这篇关于HttpServletResponse.sendError()不会重定向到错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆