这是 a.jsp
<%  String str = (String)session.getAttribute("str");
if(str == null) {
   str = "aaa";
}
out.println("<script>alert('"+str+"');</script>");
%>
<html><head></head>
<body>
<form name="myform" method="post">
<input type="button" name="ss" onclick="go()" />
</form>
</body></html>
<script>
function go(){
    myform.action = "/test";
    myform.submit();
}
</script>以下是 test servlet
dopost(...){
   session.setAttribute("str","bbb");
   out.println("<script>window.location.href='a.jsp';</script>");
}
请大家看看,第一次访问a.jsp的时候,alert 应该是 "aaa",但点击button后,为什么
alert 的还是"aaa"呢?要手动刷新一次页面,才会alert "bbb" 。请大家给点意见,谢谢!

解决方案 »

  1.   

    out.println(" <script> window.location.href='a.jsp'; </script> "); 不要用这句话用下面的试一下public void doPost(HttpServletRequest request,HttpServletResponse response)
     throws ServletException,IOException
    {        response.setContentType("text/html; charset=gb2312");
            request.setAttribute("str","bbb"); 
            ServletContext sc = getServletContext();        RequestDispatcher rd = null;        rd = sc.getRequestDispatcher("/a.jsp");     //定向的页面        rd.forward(request, response);}然后是a.jsp<%  String str = (String)request.getAttribute("str"); 
    if(str == null) { 
       str = "aaa"; 

    out.println(" <script> alert('"+str+"'); </script> "); 
    %> 
    <html> <head> </head> 
    <body> 
    <form name="myform" method="post"> 
    <input type="button" name="ss" onclick="go()" /> 
    </form> 
    </body> </html> 
    <script> 
    function go(){ 
        myform.action = "/test"; 
        myform.submit(); 

    </script>
      

  2.   

    session.setAttribute("str","bbb"); 其中的session你看是不是 HttpSession session = request.getSession()获取的.在JAVA里头打印一下bbb是否已放入Session里头.
      

  3.   

    不是怀疑,而是肯定。就是你的IE 缓冲问题造成的!清掉你的IE缓冲文件后看看吧!
    还有,可以参考这个代码,通知IE,不要缓冲
    <%
    request.setCharacterEncoding("GBK");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    %>
    <%!// return current time to proxy server request
      @Override
      public long getLastModified(@SuppressWarnings("unused")
      HttpServletRequest request) {
        return System.currentTimeMillis();
      }%>原文地址:http://www.java2000.net/viewthread.jsp?tid=288
      

  4.   

    在<head>和</head>之间加如下HTML
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">以后写jsp页面,都加这些东西。