网站统计访问次数的方法
来源:互联网 发布:银联快捷支付 网络支付 编辑:程序博客网 时间:2024/05/01 09:40
最后一种方法,session和application加文本保存结合就完美了,不管重启服务器,还是能百分百记录所有的访问记录。
写一个severlet类似前面,就是long类型改成int类型。
package com.tozhan.cn;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class Counter extends HttpServlet {
private static final long serialVersionUID = 1L;
public Counter() {
super();
}
public static void writeFile(String filename,int count){
try{
PrintWriter out=new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static int readFile(String filename) {
File f = new File(filename);
int count = 0;
if(!f.exists()){
writeFile(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(f));
try{
count = Integer.parseInt(in.readLine());
}catch(NumberFormatException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
return count;
}
public void init() throws ServletException {
// Put your code here
}
}
页面编码如下
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class Counter extends HttpServlet {
private static final long serialVersionUID = 1L;
public Counter() {
super();
}
public static void writeFile(String filename,int count){
try{
PrintWriter out=new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static int readFile(String filename) {
File f = new File(filename);
int count = 0;
if(!f.exists()){
writeFile(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(f));
try{
count = Integer.parseInt(in.readLine());
}catch(NumberFormatException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
return count;
}
public void init() throws ServletException {
// Put your code here
}
}
页面编码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.tozhan.cn.GetDataDB" %>
<%@ page import="com.tozhan.cn.news.New" %>
<%@ page import="com.tozhan.cn.Counter" %>
<%
Counter CountFileHandler=new Counter();//创建对象
int count=0;
if(application.getAttribute("count")==null){
count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");//读取文件获取数据赋给count
application.setAttribute("count",new Integer(count));
}
count=(Integer)application.getAttribute("count");
if(session.isNew()) ++count;
application.setAttribute("count",count);
CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",
count);//更新文件记录
%>
<%@ page import="com.tozhan.cn.GetDataDB" %>
<%@ page import="com.tozhan.cn.news.New" %>
<%@ page import="com.tozhan.cn.Counter" %>
<%
Counter CountFileHandler=new Counter();//创建对象
int count=0;
if(application.getAttribute("count")==null){
count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");//读取文件获取数据赋给count
application.setAttribute("count",new Integer(count));
}
count=(Integer)application.getAttribute("count");
if(session.isNew()) ++count;
application.setAttribute("count",count);
CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",
count);//更新文件记录
%>
<p>我们的友谊海枯石烂! 你是第 <%=count %> 位访客</p>
嘿嘿,,,最后一种方法是我自己总结的,利用前面找到的知识。好了,分享给大家。
转载于:http://2199572.blog.51cto.com/2189572/719181
0 0
- 网站统计访问次数的方法
- 网站统计访问次数的方法
- 网站统计访问次数的方法
- 统计网站的访问次数
- jsp网站访问次数统计的几种方法
- jsp网站访问次数统计的几种方法
- 统计一个网站的被访问次数
- python统计最近几天访问的网站次数
- js统计网站访问次数的代码
- jsp统计网站的访问次数
- 统计网站访问次数的实现
- 【代码】统计网站的访问次数
- jsp网站访问次数统计的几种方法(比较简单的几种)
- Filter应用之分ip统计网站的访问次数
- 过滤器应用之分IP统计网站的访问次数
- PHP统计网站访问次数防刷新
- JSP:用隐式对象统计网站访问次数
- 基于第三方统计工具做的网站访问次数统计。
- Android 解决升级到SDK4.4.2后Eclipse不自动生成Activity和xml布局的问题
- 【对偶定理】aizu2230
- WinDbg命令详解--线程
- MAC登陆steam玩dota2
- cocos2d-x学习笔记-触屏事件详解
- 网站统计访问次数的方法
- Java与C/C++有什么区别
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
- 有关python中的pylab的下载安装
- 如何使用Reaver破解Wi-Fi网络的WPA密码
- python 学习 1
- 设计模式Before-after之装饰模式
- Best Time to Buy and Sell Stock II
- java CMS - FreeCMS1.7 统计分析