0

我有以下问题,并想检查我是否正确。

<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 首次访问 Exam。他收到的页面显示时间=0。假设以下事件完全按照描述的顺序发生,请完成下面的 times= _ 。

  1. 用户 A 从同一浏览器窗口第二次访问 Exam。他收到回时间= _ _。

  2. 用户 B 第一次从另一台计算机访问 Exam。他收到回时间= _ _。

  3. 用户 A 第三次从另一台计算机访问 Exam。他收到回时间= _ _。

    1. 1
    2. 无效的
    3. 无效的
4

1 回答 1

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

您上面的答案是错误的,时间永远不会为空。代码所做的是在会话中存储一个整数,并在每次在同一会话中访问它时递增它。

In same session表示来自同一台计算机,来自同一浏览器,而不删除 cookie(Jsessionid)。Jsessionid cookie 由 Web 容器创建以跟踪会话。

于 2012-12-14T19:53:25.327 回答