也谈session在JSP和Servlet中不一致问题

来源:互联网 发布:碱性食物 知乎 编辑:程序博客网 时间:2024/05/21 17:43
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

    今天在处理session变量的时候,被一个session问题搞得有点头晕。

    出错情况是这样的:我本机运行没有错误,而局域网的其它机器访问我的机器时,选择第一个分类,设一个“SID”为"1"的session,转到一个页面,此时从session取出“SID”,得到"1"。接着我选择第二个分类时,设置“SID”的值为"2",这时转到另一个页面,然后从session中取出"SID",得到的值竟然还是"1"。

     我首先从后台打印来着手,但选择第二个分类时,“SID”的值还是“1”。看来是页面从ServletJSP时,session还是第一次的,没有更新。于是我增加了一个防止IE读缓存的代码: 

 response.setHeader("Cache-Control","no-store");
  response.setHeader("Pragma","no-cache");
  response.setDateHeader("Expires",0);

     问题终于得到解决。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>