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

来源:互联网 发布:苹果软件视频桌面 编辑:程序博客网 时间:2024/05/22 09:39

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>
1 0
原创粉丝点击