简体   繁体   English

在HttpServletResponse.sendError之后的过滤器中需要返回

[英]Return Needed in Filter After HttpServletResponse.sendError

I've seen some some posts that say a return is needed after the repsonse.sendError in a Servlet Filter, is it needed? 我已经看到一些帖子说在Servlet过滤器中的repsonse.sendError之后需要返回,是否需要它? If so, why? 如果是这样,为什么?

public class AuthorizationSecurityFilter extends OncePerRequestFilter {
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    boolean isAuth = //call to authenticate request
    if (isAuth) {
      chain.doFilter(request, response);
    } else {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return; //this needed?
    }
}

}

I don't know much about servlets but the return statement is actually useless and will be removed by the compiler. 我对servlet知之甚少,但return语句实际上是无用的,将被编译器删除。

The javadoc says "After using this method, the response should be considered to be committed and should not be written to." javadoc说“在使用这种方法之后,应该认为响应已经提交,不应该写入。” I guess you are referring to this. 我猜你指的是这个。

Imagine this snippet: 想象一下这个片段:

if( anyErrorCondition(request) ) {
    response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
// Further processing

In this case, you should definitely use a return statement after calling sendError because you want to prevent further processing. 在这种情况下,您应该在调用sendError之后使用return语句,因为您希望阻止进一步处理。

This does not apply to your example which does not process the request after sending the error code but will instead reach the end of the method and return. 这不适用于您的示例,该示例在发送错误代码后不处理请求,而是到达方法的结尾并返回。

Summing up: No, you don't need a return statement here, as long as you can guarantee that the request will not be processed after calling sendError . 总结:不,这里不需要return语句,只要您可以保证在调用sendError之后不会处理该请求。

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

相关问题 HttpServletResponse.sendError()不会重定向到错误页面 - HttpServletResponse.sendError() does not redirect to error page 如何将消息从 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或setStatus - Override HttpServletResponse sendError or setStatus Mockito:捕获HttpServletResponse#sendError() - Mockito: capturing HttpServletResponse#sendError() HttpServletResponse#sendError()是否会抛出IOException吗? - Does HttpServletResponse#sendError() ever throw an IOException? Spring异常处理 - @ControllerAdvice无法处理HttpServletResponse#sendError() - Spring Exception Handling - @ControllerAdvice cannot handle HttpServletResponse#sendError() JBoss如何实现HttpServletResponse setStatus()与sendError() - How does JBoss implement HttpServletResponse setStatus() vs sendError() HttpServletResponse .sendError 在缺少 CSRF 令牌时返回空响应正文 - HttpServletResponse .sendError returns empty response body when missing CSRF token 将响应(HttpServletResponse)返回到HttpServletRequest - Return Response (HttpServletResponse) to HttpServletRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM