Can't sendRedirect() after data has committed to the client

时间:2023-03-08 22:52:13
Can't sendRedirect() after data has committed to the client

resin下

response.sendRedirect("XXX");

会报异常:java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.

原因是在做登陆时用户名和密码判断后,做的页面跳转有问题,按照异常来理解,应该是服务器已经返回了状态200或者404或者其他的状态码,但是又执行了重定向的代码,返回302状态码。服务器同一次相应应该只返回一种状态,response.sendRedirect执行后,应用服务器还是会执行接下去的代码的,所以一般都可以通过return语句中断

如果没有中断,而且接下去又有返回状态码的语句时,就会报这个错