首页 > 代码库 > 有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常

java.lang.IllegalStateException: Cannot create a session after the response has been committed

 

原因1: 

Session 的创建语句: HttpSession seesion = request.getSession(); 之前有Response的输出语句。

应该把HttpSession seesion = request.getSession(); 放在Response的输出语句前面

 

例如:


response.sendRedirect("xxx.jsp");

HttpSession session = req.getSession();

改成

HttpSession session = req.getSession();

response.sendRedirect("xxx.jsp");

 

原因2:

如果使用了Struts2可以在struts.xml中添加一个默认的拦截器:

            <interceptor-ref name="createSession"/>
            <interceptor-ref name="defaultStack"/>