JS+JSP通過(guò)img標(biāo)簽調(diào)用實(shí)現(xiàn)靜態(tài)頁(yè)面訪問(wèn)次數(shù)統(tǒng)計(jì)的方法
本文實(shí)例講述了JS+JSP通過(guò)img標(biāo)簽調(diào)用實(shí)現(xiàn)靜態(tài)頁(yè)面訪問(wèn)次數(shù)統(tǒng)計(jì)的方法。分享給大家供大家參考,具體如下:
測(cè)試頁(yè)面: test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</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>
<body>
this is a test page.
<script type="text/javascript">document.write("<img src=http://127.0.0.1:8080/EasyCMS/pv.jsp border=0 width=0 height=0>");</script>
</body>
</html>
統(tǒng)計(jì)程序: pv.jsp:
假設(shè)部署位置為http://127.0.0.1:8080/EasyCMS/pv.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%
String path="/opt/test.txt";
writeNumber(String.valueOf(readNumber(path)+1),path);
%>
<%=readNumber(path) %>
<%!
/**
* 寫入數(shù)字內(nèi)容
*
* @param number
* @param filename
* @return
*/
public boolean writeNumber(String number, String filename) {
try {
FileOutputStream fos = new FileOutputStream(filename);
OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.write(number);
writer.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 讀取數(shù)字內(nèi)容
*
* @param filename
* @return
*/
public int readNumber(String filename) {
int number = 0;
try {
File file = new File(filename);
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String contents = br.readLine();
if (contents != null && contents.length() > 0) {
contents = contents.replaceAll("[^0-9]", "");
number = Integer.valueOf(contents);
}
br.close();
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return number;
}
%>
基本思想:
訪問(wèn)靜態(tài)頁(yè)面時(shí),通過(guò)img標(biāo)簽指定src 為訪問(wèn)統(tǒng)計(jì)的地址, img標(biāo)簽向統(tǒng)計(jì)程序發(fā)出請(qǐng)求,實(shí)現(xiàn)統(tǒng)計(jì).
統(tǒng)計(jì)示例代碼采用文件來(lái)記錄訪問(wèn)次數(shù),實(shí)際項(xiàng)目可以記錄數(shù)據(jù)庫(kù).
關(guān)鍵代碼:
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- jsp利用echarts實(shí)現(xiàn)報(bào)表統(tǒng)計(jì)的實(shí)例
- JSP實(shí)現(xiàn)網(wǎng)頁(yè)訪問(wèn)統(tǒng)計(jì)
- jsp利用application統(tǒng)計(jì)在線人數(shù)的方法
- jsp的九大內(nèi)置對(duì)象深入講解
- springboot整合jsp,實(shí)現(xiàn)公交車站路線圖
- SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn)
- 如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目
- idea springboot 修改css,jsp不重啟實(shí)現(xiàn)頁(yè)面更新的問(wèn)題
- Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理
- 教你怎么用JSP統(tǒng)計(jì)網(wǎng)站訪問(wèn)人數(shù)
相關(guān)文章
javascript函數(shù)定義的幾種區(qū)別小結(jié)
本篇文章主要是對(duì)javascript函數(shù)定義的幾種區(qū)別進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
JavaScript如何實(shí)現(xiàn)元素全排列實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于JavaScript如何實(shí)現(xiàn)元素全排列的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
react-router-dom?v6?通過(guò)outlet實(shí)現(xiàn)keepAlive?功能的實(shí)現(xiàn)
本文主要介紹了react-router-dom?v6?通過(guò)outlet實(shí)現(xiàn)keepAlive功能,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
JavaScript實(shí)現(xiàn)仿新浪微博大廳和騰訊微博首頁(yè)滾動(dòng)特效源碼
最近看到朋友用JavaScript實(shí)現(xiàn)仿新浪微博大廳和未登錄騰訊微博首頁(yè)滾動(dòng)效果,朋友使用jquery實(shí)現(xiàn)的,在網(wǎng)上看到有用js制作的也比較好,于是把我的內(nèi)容整理分享給大家,具體詳解請(qǐng)看本文2015-09-09
for 循環(huán)性能比較 提高for循環(huán)的效率
性能有較大的區(qū)別嗎,有必要采取這種寫法嗎2009-03-03
JavaScript Reflect Metadata實(shí)現(xiàn)詳解
這篇文章主要介紹了JavaScript Reflect Metadata實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

