网站统计访问次数的方法

最后一种方法,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
    }

}
页面编码如下
<%@ 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);//更新文件记录
%>
    <p>我们的友谊海枯石烂! 你是第&nbsp;<%=count %>&nbsp;位访客</p>

嘿嘿,,,最后一种方法是我自己总结的,利用前面找到的知识。好了,分享给大家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现统计网站2023每日的访问次数,可以使用Hadoop中的MapReduce框架。具体步骤如下: 1. 将网站2023的访问日志文件上传至Hadoop分布式文件系统中。 2. 编写MapReduce程序,其中Mapper阶段将每条访问记录解析成日期和访问次数键值对,Reducer阶段将相同日期的访问次数累加起来。 3. 运行MapReduce程序,得到每日的访问次数统计结果。 以下是示例代码: Mapper部分: ```java public class AccessLogMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); private final static IntWritable ONE = new IntWritable(1); private Text date = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split("\t"); if (fields.length >= 3) { try { Date timestamp = sdf.parse(fields[0]); date.set(sdf.format(timestamp)); context.write(date, ONE); } catch (ParseException e) { // ignore bad records } } } } ``` Reducer部分: ```java public class AccessLogReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable totalCount = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable value : values) { count += value.get(); } totalCount.set(count); context.write(key, totalCount); } } ``` 运行命令: ```bash hadoop jar access-log.jar AccessLog /input/access.log /output ``` 其中,`access-log.jar`是打包好的MapReduce程序,`AccessLog`是程序的入口类,`/input/access.log`是输入文件路径,`/output`是输出文件路径。最终的统计结果将保存在`/output`目录下的结果文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值