2012-10-14 44 views
0

什麼,我試圖做的是相對簡單的
在我的web應用程序有這兩個servlet:
(我會寫一些僞)會議servlet和href標記

servlet的一段代碼:

HttpSession sess = req.getSession(); 

    String str = (String) sess.getAttribute("log"); 

    if(str == null) 
    { 
    // send html page with a form 
    // to enter password and name 
    // data will be proceessed by servlet B 
    } 
    else 
    { 
    // send html page with a form 
    // to enter only a name 
    //data will be proceessed by servlet B 
    } 

servlet的B碼:

 HttpSession sess = req.getSession(); 

     String logged = (String) sess.getAttribute("log"); 

     if(logged == null) 
     { 

     //check if password correct 

      if(correct) 
      { 
      sess.setAttribute("log","ok"); 

      // print name 
      // and tell the user 
      // that next time password 
      // will not be requested 
      } 
      else 
      { 
      // print error message 
      } 

     } 
     else 
     { 
     // print name 
     } 

由於某些原因,第二次的servlet A被後填寫密碼,該用戶名正確 STR所以如果部分被稱爲印刷。

編輯:

我發現,如果將密碼和 被重定向到servletB後用戶自己 寫servletA的URL一切正常,但是當用戶回來吧odesn't工作以前 頁面使用servletA創建一個鏈接: A HREF = \ 「HTTP://本地主機:8080 /博客/ servletA \」 回

再次,任何建議,爲什麼出現這種情況?

回答

0

我相信它沒有輸入你的if(correct)區塊,即correct的條件沒有得到滿足。修正那裏的情況並確保它在session中設置log屬性。一旦將屬性設置到會話中,它會一直到會話過期或您將其刪除。