can not create a session after response is commited

来源:互联网 发布:北京索顿网络是培训吗 编辑:程序博客网 时间:2024/06/12 21:45

can not create a session after response is commited

这个错误一般在本地难以发生,部署到服务器上回发生。

1.getsession()放在了response提交后面

2.getssion(true),没有session会创建session,访问量稍大时,在多线程环境,单例模式下(Spring默认创建bean的单例的,做权限管理用拦截器时,HandlerInterceptor也是单例),可能会出现response提交了,session再创建,因此不能用true

3.response返回数据是有缓存限制的,具体参考下面文章,当缓存超出限制是就会自动提交的

You shouldn't be doing business job in getters. Do it in the bean (post)constructor instead.

Your concrete problem is caused because you're requesting a relatively large page on a fresh new browser session for which the server HttpSession hasn't been created yet and the EL expression #{equityBean.scripList} is referenced relatively late in the page.

The response buffer is by default 2KB and when it overflows due to a large response, then it will be committed. This means that all response headers will be sent and that first ~2KB of the HTML output will be sent. Then, after that point, the EL expression #{equityBean.scripList} will be resolved wherein you're trying to get the session. If the server HttpSession hasn't been created yet at that moment, then the server needs to set a cookie in the response header in order to maintain it for subsequent requests. But that's of course not possible if the response has already been committed. Hence this exception.

As said, just do the job in bean's (post)constructor instead. Or just inject it as managed property.

@ManagedProperty("#{type}")private String type;
------https://stackoverflow.com/questions/10949556/cannot-create-session-after-response-has-been-committed。
阅读全文
0 0
原创粉丝点击