2012-12-14 59 views
0

我有以下問題,並且想檢查我是否正確。Jsp會話值

<title>Exam</title> 
</head> 
<body> 
<% Integer times = (Integer)(session.getAttribute("times")) ; 
if (times == null) { times = new Integer(0) ; } 
else { times = new Integer(times.intValue() + 1) ; 
session.setAttribute("times", times) ;} 
%> 
times = <%=times %> 

用戶A首次訪問考試。他收到的頁面顯示times = 0。完成下面的時間= _,假設以下事件完全按照所描述的順序發生。

  1. 用戶A在同一瀏覽器窗口中進行第二次訪問考試。他收到回 次= _ _。

  2. 用戶B從另一臺計算機首次訪問考試。他收到回退時間= _ _。

  3. 用戶A再次從另一臺計算機訪問考試。他收到回 次= _ _。

回答

1

假設你閉上你的else塊

<title>Exam</title> 
</head> 
<body> 
<% Integer times = (Integer)(session.getAttribute("times")) ; 
    if (times == null) { times = new Integer(0) ; } 
    else { times = new Integer(times.intValue() + 1) ;} 
    session.setAttribute("times", times) ; 
%> 
times = <%=times %> 

上面你的答案是錯的,時間永遠不會爲空。代碼的做法是在會話中存儲一個整數,並在每次在同一會話中訪問時遞增它。

In same session表示從同一臺計算機,從相同的瀏覽器,沒有刪除cookie(Jsessionid)。 Jsessionid cookie由Web容器創建以跟蹤會話。

+0

你的意思是通過關閉else塊,我認爲問題是要求「接收回來的時間」是session.getAttribute(「times」)應該是「null」,然後第二個用戶從其他計算機進入? – keivn

+0

上面的代碼將導致編譯錯誤,因爲你的else塊沒有關閉使用} – Subin

+0

固定,但你能回答我的第二個問題嗎? – keivn