【问题标题】:Jsp session valueJsp会话值
【发布时间】:2012-12-02 20:12:32
【问题描述】:

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

<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

【问题讨论】:

    标签: javascript ajax jsp session servlets


    【解决方案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

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

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

    【讨论】:

    • 关闭 else 块是什么意思,我认为问题是询问“接收回时间”,即 session.getAttribute("times") 应该是“null”,然后第二个用户从其他用户输入电脑?
    • 以上代码将导致编译错误,因为您的 else 块未使用 } 关闭
    • 已修复,但您能回答我的第二个问题吗?
    • 实际上你应该像我展示的那样关闭它。如果您像现在一样关闭,那么您将得到 0,对于所有用户,无论是否有时间,因为我们没有在会话中存储任何内容。
    • 所以,如果我像你所做的那样关闭,那么用户B 从另一台计算机访问此页面,当代码“整数次 = (整数)(session.getAttribute(“次”));“时间=0? , 与另一台计算机上的 UserA 相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多