Project scenario:
Spring boot project, user-defined filter, intercept requests and process business.
Problem Description:
When you start the project access interface, you call resp.sendRedirect () in the custom filter.
Cause analysis:
After the response has been submitted, the sendredirect() method cannot be called.
In fact, chain. Dofilter (req, resp) is executed; Cannot use sendredirect() of resp object after
So looking at the code, we find that:
...
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
....
boolean flag = getStatus();
if (flag) {
....
chain.doFilter(request, response);
}
httpResponse.sendRedirect(“xxxx”);
...
if the judged flag is true, chain.dofilter (request, response) will be executed, and httpresponse.sendredirect (“XXXX”) </ font> will be executed after execution
Solution:
After chain. Dofilter (request, response), add return to end the current method.
...
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
....
boolean flag = getStatus();
if (flag) {
....
chain.doFilter(request, response);
return;
}
httpResponse.sendRedirect(“xxxx”);
...
Read More:
- Cannot call sendRedirect() after the response has been committed
- [Redirect anomaly] Cannot call sendRedirect() after the response has been committed
- [Solved] cannot call sendError() after the response has been committed
- [Solved] Cannot call sendError() after the response has been committed
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- Caused by: java.lang.IllegalStateException (How to Fix)
- [Solved] Internal error (java.lang.IllegalStateException): Duplicate key org.jetbrains
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- [Solved] Java.lang.IllegalStateException: Type handler was null on parameter mapping for property ‘__frch_it
- JUnit test classes error: java.lang.IllegalStateException: Failed to load ApplicationContext
- [Solved] Java.lang.BootstrapMethodError: call site initialization exception
- Gitee Idea Push Error: Invocation failed Server returned invalid Response. java.lang.RuntimeException
- [Solved] Logstash Error: Logstash – java.lang.IllegalStateException: Logstash stopped processing because of an err
- [Solved] Openfegn Remote Call Error: java.lang.NullPointerException: null
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] nacos Startup Error: nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error
- [Solved] Java 9 reflection error: java.lang.reflect.InaccessibleObjectException
- [Solved] JAVA Project Import jstl Error: java.lang.NoClassDefFoundError: javax/servlet/jsp/tagext/TagLibraryValidator
- [Solved] java: Internal error in the mapping processor: java.lang.NullPointerException