A5下载文章资讯

分类分类

JS+JSP通过img标签调用实现静态页面访问次数统计的方法

2015-12-14 14:22作者:fang

本文实例讲述了JS+JSP通过img标签调用实现静态页面访问次数统计的方法。分享给大家供大家参考,具体如下:

测试页面:test.html

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<title>test</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

<!--

<linkrel="stylesheet"type="text/css"href="styles.css">

-->

</head>

<body>

thisisatestpage.

<scripttype="text/javascript">document.write("<imgsrc=http://127.0.0.1:8080/EasyCMS/pv.jspborder=0width=0height=0>");</script>

</body>

</html>

统计程序:pv.jsp:

假设部署位置为http://127.0.0.1:8080/EasyCMS/pv.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@pageimport="java.io.*"%>

<%

Stringpath="/opt/test.txt";

writeNumber(String.valueOf(readNumber(path)+1),path);

%>

<%=readNumber(path)%>

<%!

/**

*写入数字内容

*

*@paramnumber

*@paramfilename

*@return

*/

publicbooleanwriteNumber(Stringnumber,Stringfilename){

try{

FileOutputStreamfos=newFileOutputStream(filename);

OutputStreamWriterwriter=newOutputStreamWriter(fos);

writer.write(number);

writer.close();

fos.close();

}catch(IOExceptione){

e.printStackTrace();

returnfalse;

}

returntrue;

}

/**

*读取数字内容

*

*@paramfilename

*@return

*/

publicintreadNumber(Stringfilename){

intnumber=0;

try{

Filefile=newFile(filename);

if(file.exists()){

FileReaderfr=newFileReader(file);

BufferedReaderbr=newBufferedReader(fr);

Stringcontents=br.readLine();

if(contents!=null&&contents.length()>0){

contents=contents.replaceAll("[^0-9]","");

number=Integer.valueOf(contents);

}

br.close();

fr.close();

}

}catch(IOExceptione){

e.printStackTrace();

}

returnnumber;

}

%>

基本思想:

访问静态页面时,通过img标签指定src为访问统计的地址,img标签向统计程序发出请求,实现统计.

统计示例代码采用文件来记录访问次数,实际项目可以记录数据库.

关键代码:

代码如下:

<scripttype="text/javascript">document.write("<imgsrc=http://127.0.0.1:8080/EasyCMS/pv.jspborder=0width=0height=0>");</script>

希望本文所述对大家JavaScript程序设计有所帮助。

展开全部

相关

说两句网友评论
    我要跟贴
    取消