2012-12-14 52 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