Cannot create a session after the response has been committed
有時候在操作Session時,系統(tǒng)會拋出如下異常
java.lang.IllegalStateException: Cannot create a session after the response has been committed
之所以會出現(xiàn)此類問題是因為我們在Response輸出響應(yīng)后才創(chuàng)建Session的。
(因為那時候服務(wù)器已經(jīng)將數(shù)據(jù)發(fā)送到客戶端了,即:就無法發(fā)送Session ID 了)
解決辦法:
你只需要在你的程序中將創(chuàng)建訪問Session的語句【request.getSession()】提前至Response輸出數(shù)據(jù)之前就好了。
例如改成下面的寫法OK:
ServletOutputStream out = response.getOutputStream();
// 最好這樣緊挨著 response.getOutputStream()
HttpSession seesion = request.getSession();
seesion.setAttribute("xxx", rand);
// 輸出數(shù)據(jù)
out.print("<h1>hello</h1>");
out.close();
http://hi.baidu.com/matrix286/item/63085d33459a0027b3c0c565
轉(zhuǎn)載于:https://blog.51cto.com/fzqmcn/1270679
總結(jié)
以上是生活随笔為你收集整理的Cannot create a session after the response has been committed的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZOJ 3430 Detect the
- 下一篇: css工作笔记