简体   繁体   English

HttpServletResponse.sendError()不会重定向到错误页面

[英]HttpServletResponse.sendError() does not redirect to error page

I have the following code in doFilter() method, where I get application scoped bean. 我在doFilter()方法中有以下代码,在这里我获得了应用程序作用域bean。

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

I mapped 503 code to specific error page in web.xml. 我将503代码映射到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. 但地址栏中的地址不会因错误页面地址而改变:请求的servlet的地址离开那里。 Is it right behaviour? 这是正确的行为吗? I'd like to inform explicitly about redirection to error page. 我想明确告知重定向到错误页面。 Is it only possible with sendRedirect()? 是否只能使用sendRedirect()?

This is the correct behavior. 这是正确的行为。 When you use sendError() it will respond to the current request with an error page. 当您使用sendError()时,它将使用错误页面响应当前请求。 If you instead want the URL to change to the error page URL, you will need to use sendRedirect() to respond with a redirect. 如果您希望将URL更改为错误页面URL,则需要使用sendRedirect()来响应重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在HttpServletResponse.sendError之后的过滤器中需要返回 - Return Needed in Filter After HttpServletResponse.sendError 如何将消息从 HttpServletResponse.sendError(status, msg) 传递给控制器​​? - How to pass the msg from HttpServletResponse.sendError(status, msg) to controller? 我如何从HttpServletResponse.sendError检索味精 - how can i retrieve the msg from HttpServletResponse.sendError HttpServletResponse#sendError()是否会抛出IOException吗? - Does HttpServletResponse#sendError() ever throw an IOException? JBoss如何实现HttpServletResponse setStatus()与sendError() - How does JBoss implement HttpServletResponse setStatus() vs sendError() 重写HttpServletResponse sendError或setStatus - Override HttpServletResponse sendError or setStatus Mockito:捕获HttpServletResponse#sendError() - Mockito: capturing HttpServletResponse#sendError() 当从servlet使用sendError时,tomcat不显示错误消息 - tomcat does not show error message when sendError used from servlet Spring异常处理 - @ControllerAdvice无法处理HttpServletResponse#sendError() - Spring Exception Handling - @ControllerAdvice cannot handle HttpServletResponse#sendError() HttpServletResponse .sendError 在缺少 CSRF 令牌时返回空响应正文 - HttpServletResponse .sendError returns empty response body when missing CSRF token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM