keivn
keivn

Reputation: 114

Jsp session value

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.

  1. User A makes his second access to Exam from the same browser window. He receives back times=__.

  2. User B makes his first access to Exam from another computer. He receives back times=__.

  3. User A makes his third access to Exam from yet another computer. He receives back times=__.

    1. 1
    2. null
    3. null

Upvotes: 0

Views: 454

Answers (1)

Subin Sebastian
Subin Sebastian

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 %>
  1. 1
  2. 0
  3. 0

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

Related Questions