2012-03-09 130 views
0

我試圖做一個Java Web應用程序,用戶可以登錄到一個Java Web應用程序,我創建的網站上訪問一個頁面certian。我在下面的代碼中檢查用戶的電子郵件和密碼是否存在於我的成員數據庫中。會話變量沒有被設置

然而,當我嘗試設置SEESION變量爲null。

try { 
     Class.forName(driver).newInstance(); 
     conn = DriverManager.getConnection(url, userName, password); 
     if (request.getParameter("email") != null && !request.getParameter("email").equals("") && request.getParameter("password") != null && !request.getParameter("password").equals("")) { 
      email = request.getParameter("email").toString(); 
      userpass = request.getParameter("password").toString(); 
      strQuery = "SELECT * FROM member WHERE email = ? and password =?"; 
      PreparedStatement stmt = conn.prepareStatement(strQuery); 
      stmt.setString(1, email); 
      stmt.setString(2, userpass); 


      System.out.println(stmt); 
      rs = stmt.executeQuery(); 
      int count = 0; 
      while (rs.next()) { 
       session.setAttribute("email", rs.getString(2)); 
       System.out.println(session.getAttribute(email)); //This is null in system console it should not be 
       count++; 
      } 
      if (count > 0) { 
       response.sendRedirect("index.jsp"); 
      } else { 
       response.sendRedirect("index.jsp"); 
      } 
     } else { 
      response.sendRedirect("gourmet.jsp"); 
     } 
     System.out.println("Connected to the database"); 
     conn.close(); 
     System.out.println("Disconnected from database"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

回答

3

您的代碼中存在一個簡單的語法錯誤。您的會話變量需要在引號內。 試試這個:

System.out.println(session.getAttribute("email"));