[英]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.