<%@ page language="java" import="java.util.*,example.Configuration" pageEncoding="UTF-8"%>
<%
Configuration conf = new Configuration();
String getSeeesionId = (String) session.getAttribute("sessionId");
String sessionId = session.getId();
String visitNum = (String) session.getAttribute("visitNum");
if (!sessionId.equals(getSeeesionId)) {
//更新訪問數(shù)量
conf.setValue();//獲取更新后的訪問數(shù)量
visitNum = conf.getValue("visitNum");
}
session.setAttribute("sessionId", sessionId);
session.setAttribute("visitNum", visitNum);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>瀏覽量統(tǒng)計(jì)</title>
</head>
<body>
您是第<%=visitNum%>個(gè)訪問者
</body>
</html>
RecordCount.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;public class RecordCount
{
//使用Properties
private static Properties pp=new Properties();
public static void writeCount(String path,String count)
{
pp.setProperty("count", count); //設(shè)置鍵值對
try {
pp.store(new FileOutputStream(path), ""); //寫入文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readCount(String path)
{String count="0";
File f = new File(path);
if (!f.exists())
{
writeCount(path, "0");
}
try {
pp.load(new FileInputStream(path));
count=pp.getProperty("count");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
}jsp 頁面:
<%@page import="cn.zgcyx.RecordCount"%>
<body>
<!--在需要的地方插入下面代碼-->
<%
String count = RecordCount.readCount("C:/info1.txt");
if (session.getAttribute("visit") == null) {
session.setAttribute("visit", "y");//將未訪問設(shè)置為訪問
session.setMaxInactiveInterval(60 * 60 * 24);//設(shè)置最大時(shí)效 單位是秒int count1 = Integer.parseInt(count);
count1 = count1 + 1;
count = String.valueOf(count1).toString();
RecordCount.writeCount("C:/info1.txt", count);}
%></body>