yc451075935 2014-10-24 09:23 采纳率: 0%
浏览 3858

为什么session.isNew()刷新页面后总会返回TRUE

小白最近刚接触JSP,在用session对象写一个防刷新计数器时遇到了问题。理想情况下是刷新页面时计数器不+1,仅当关闭浏览器再重新打开时才会+1;可是在Eclipse JavaEE的内部浏览器测试时刷新网页计数器也会+1,不知道出了什么问题,下面贴出代码,恳请各位前辈指教

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%!
        int number=0;
        synchronized void countpeople(){
        number++;
    }
    %>
    <%
        if (session.isNew()){
            countpeople();
            String num=String.valueOf(number);
            session.setAttribute("counter", num);
        }
    %>
    您是第<%=session.getAttribute("counter")%>位访问者!
</body>
</html>
  • 写回答

2条回答

  • Mr阿龙 2015-11-22 03:46
    关注

    你不应该用session,你要用application;
    代码
    <%
    Integer yourNumber = new Integer(0);
    %>
    if(session.isNew()){
    Integer number = (Integer)application.getAttribute("Count");
    if(number == null){
    number = new Integer(1);
    }
    else{
    number = new Integer(number.intValue()+1);
    }
    application.setAttribute("Count",number);
    yourNumber = (Integer)application.getAttribute("Count");

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler