有时候在操作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"/>

posted on 2014-07-04 16:56  wyw218  阅读(1682)  评论(0)    收藏  举报

导航