您的位置:首页 > 运维架构 > 网站架构

JSP:用隐式对象统计网站访问次数

2015-04-26 09:47 459 查看


JSP:用隐式对象统计网站访问次数

jsp
隐式对象
赵振江


隐式对象 application对象

利用隐式对象为某一网站编写一个JSP程序,统计该网站的访问次数。

一种情况是:按照客户进行统计(按照浏览器进行统计,一个浏览器如果访问网站的话,就算一次访问,换句话说如果这个浏览器刷新多次网站的话,也算是一次访问);

另一种情况:刷新一次页面,就算是一次访问。

要求用隐式对象去实现。

counter.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'counter.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <%
  if(application.getAttribute("counter")==null){
    String counter="0";
    application.setAttribute("counter",counter);
  }
  else{
  int count=Integer.valueOf((String)application.getAttribute("counter")).intValue();
  count++;
  application.setAttribute("counter",count+"");
  }
   %>

  <body>
    This is my JSP page. <br>

    该网站共被访问:<%=application.getAttribute("counter") %>次。
  </body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: