Reputation: 114
I have the following question, and want to check if i got it right or not.
<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 %>
User A makes the first access to Exam. The page he receives back says times=0. Complete the times=_ below, assuming the following events happen in exactly the sequence described.
User A makes his second access to Exam from the same browser window. He receives back times=__.
User B makes his first access to Exam from another computer. He receives back times=__.
User A makes his third access to Exam from yet another computer. He receives back times=__.
Upvotes: 0
Views: 454
Reputation: 10997
Assuming you close your else block
<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 %>
Your answer above is wrong, times will never be null. What code is doing is storing a integer in session and incrementing it each time you access it in same session.
In same session
means from same computer, from same browser, without deleting cookie(Jsessionid). Jsessionid cookie is created by web container to track sessions.
Upvotes: 1