怎么在有人访问我的网站的时候,记录下来总的访问人数,并在网页中显示出来???哪种方式最省时???

点个赞吧 2010-07-14 10:55:53
怎么在有人访问我的网站的时候,记录下来总的访问人数,并在网页中显示出来???有人说要建一个数据库专门记录访问量,有人说建库比较耗时。。。。到底什么方式最好???
...全文
488 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyx1314LCJ 2010-07-20
  • 打赏
  • 举报
回复
用application对象实现网页计数器

<%--用application对象实现网页计数器peixun2.14.jsp文件代码--%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>网页计数器</title><head>
<body>
<% if (application.getAttribute("counter")==null)
application.setAttribute("counter","1");
else{
String strnum=null;
strnum=application.getAttribute("counter").toString();
int icount=0;
icount=Integer.valueOf(strnum).intValue();
icount++;
application.setAttribute("counter",Integer.toString(icount));
} %>
您是第<%=application.getAttribute("counter")%>位访问者!
</body>
</html>
wzglovejava 2010-07-20
  • 打赏
  • 举报
回复
最好是用HttpSessionListener,不过这个也要看你的需求了,你可以百度一下,有关这个的解决方案有很多很多。。
cheng20100915 2010-07-19
  • 打赏
  • 举报
回复
application 实现计数器
lxf001002 2010-07-19
  • 打赏
  • 举报
回复
http://www.javaeye.com/wiki/topic/690278
这个讲的很详细 全部配置说明和例子
chens_1218 2010-07-14
  • 打赏
  • 举报
回复
httpSessionListener毋庸置疑
dcjz 2010-07-14
  • 打赏
  • 举报
回复
HttpSessionListener
紫眸少年丶 2010-07-14
  • 打赏
  • 举报
回复
呵呵 这是我用struts做的一个简单计数 你可以参考下 如果有更好的麻烦通知下谢谢
public class MyCount extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String user = request.getParameter("user");
int newCount = (Integer) session.getAttribute(user) == null ? 1
: (Integer) session.getAttribute(user);
session.setAttribute("user", user);
if (session.getAttribute(user) != null) {
session.setAttribute(user, newCount + 1);
}else{
session.setAttribute(user, newCount);
}
response.sendRedirect("showCount.jsp");
}

}
iamrf 2010-07-14
  • 打赏
  • 举报
回复
当然你的要求只是要累积的访问人数,只增不减,怎么样都可以。只要你的数会增加,一般项目经理不去关心统计的数字正确与否。

如果你要精确控制当前系统的在线人数,就得根据你自己的判定要求去实现我刚才说的那两个接口。
iamrf 2010-07-14
  • 打赏
  • 举报
回复
如果你的访问判定标准是:只要有一个请求去了你的服务器(包括请求登陆和请求图片),就算访问的话,那就用HttpSessionListener接口进行监听。

如果你的访问判定为登陆后或者满足其他什么要求才算访问的话,用HttpSessionBindingListener接口监听登陆操作。因为登陆肯定要向session中绑定用户。如果是其他什么要求,当用户操作满足时,你可以向session中绑定特定参数,然后在HttpSessionBindingListener接口中监听特定的绑定动作来计数。


当然前面的方法也是可取的,但如果你想精确的根据你自己的访问规则控制,用application跟session我只能说是相当不准确的!
bastengao 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyongai0563 的回复:]

用application 在启动项目的时候还原 在关闭的时候持久化
[/Quote]好方法
closewbq 2010-07-14
  • 打赏
  • 举报
回复
wuyongai0563 2010-07-14
  • 打赏
  • 举报
回复
用application 在启动项目的时候还原 在关闭的时候持久化
closewbq 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kingshigang 的回复:]
application
[/Quote]
+1
application实现个计数器。非常简单。
kingshigang 2010-07-14
  • 打赏
  • 举报
回复
application
liuyuhua0066 2010-07-14
  • 打赏
  • 举报
回复
session

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧