没有任何问题
楼主是不是因为没有在页面中初始化这个session?
所以在第一次调用的时候,会说“初始化变量”

解决方案 »

  1.   

    刷新?
    会不会是jsp页面有问题?
      

  2.   

    我直接用servlet,不用jsp显示页面
      

  3.   


    改称:  public void init() throws ServletException {
      }  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();    HttpSession session = request.getSession(true);//利用session中的isFirst判断是否第一次进入此页面。
        if (session.isNew() || session.getAttribute("isFirst") == null) {
          System.out.println("初始化变量");
          session.setAttribute("isFirst", "1");
          }
        else{
          System.out.println("变量已经进行初始化");
        }
      }  }
      

  4.   

    public Object getAttribute(String name)Returns the object bound with the specified name in this session, or null if no object is bound under the name. 
    public boolean isNew()Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request. Returns:
    true if the server has created a session, but the client has not yet joined 你session.isNew() 多余
      

  5.   

    如果不用session.isNew() ,就是你想要的
      

  6.   

    我这里测试已经通过
    说明不是session传递的问题
    我估计是楼主程序中其他地方引起
    这段代码本身应该没有问题
      

  7.   

    问题解决了,程序的其他地方都没有问题,我将Tomcat重起就一切ok啦。劳烦各位了^_^