页面点击量怎么计算?
a.jsp
怎么记录每天的点击量?
记录数实时存入数据库还是文件还是内存?
如果存入内存,服务器关闭时调用什么方法来把记录数写入数据库或文件?
------解决方案--------------------写入文件的方法
counter.txt 中内容初始设为1
counter.jsp 代码如下
<%@ page language= "java " contentType= "text/html; charset=gbk "
pageEncoding= "gbk "%>
<%@ page import= "java.io.* " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gbk ">
<title> 普通文本计数器 </title>
</head>
<body>
<div align = "center ">
<h1> 普通文本计数器 </h1>
<%
String sFilename =
getServletConfig().getServletContext().getRealPath( "counter.txt ");
String sCount = null;
int iCount = 0;
BufferedReader BRobj = new BufferedReader(new FileReader(sFilename));
try {
sCount = BRobj.readLine();
} catch(
IOException e) {
e.printStackTrace();
out.println( " <p> <font color = red> 读取计数器文本数据出错! </font> </p> ");
return;
}
if(sCount == null) {
out.println( " <p> <font color = red> 计数器文本无数据! </font> </p> ");
return;
}
else
{
try
{
iCount = Integer.parseInt(sCount) + 1;
}
catch(
NumberFormatException e) {
out.println( " <p> <font color = red> 计数器文本数据出错! </font> </p> ");
return;
}
}
%>
<p> 您是本站的第 <font color = "#FF0000 "> <b> <%=iCount %> </b> </font> 位访问者 </p>
<%
try
{
PrintWriter PWobj = new PrintWriter(new FileOutputStream(sFilename));
PWobj.println(iCount);
PWobj.close();
} catch(IOException e) {
out.println( " <p> <font color = red> 写入计数器文本数据出错! </font> </p> ");
return;
}
%>
</div>
</body>
</html>
------解决方案--------------------路过。。。。用容器监听吧