编写一个servle能够t获取对象访问浏览器对象次数,并在重启服务器之后,次数不变继续增加

1. 创建servlet继承HttpServlet

public class CountVisitTime extends HttpServlet {

    @Override
    protected  synchronized void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行service方法.....");

2. 在WEB-INF下创建文件count记录次数并在servlet中创建文件路,路径名为 count文件的路径

   //获取文件路径
        String realPath=req.getServletContext().getRealPath("/WEB-INF/count");
        File file=new File(realPath);
        System.out.println(realPath);

3. 创建properties文件

 Properties properties=new Properties();
        FileInputStream fileInputStream=new FileInputStream(file);
        //加载存数据的properties文件
        properties.load(fileInputStream);
        fileInputStream.close();

4. 获取properties文件中的count数量

 System.out.println(properties);
        //获取之前的浏览量
        String s=properties.getProperty("count");
        //声明一个存浏览量的变量
        int num=0;
        try {
            //更改浏览量
           num=Integer.parseInt(s);
           num++;
        }catch (Exception e){
            e.printStackTrace();
        }

5. 获取浏览量后更新输出

//更新浏览量
        properties.setProperty("count", String.valueOf(num));
        FileOutputStream fileOutputStream=new FileOutputStream(realPath);
        //注释部分
        properties.store(fileOutputStream, "successful");
        System.out.println("当前浏览量"+num);
完整代码
package com.etime.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Properties;


public class CountVisitTime extends HttpServlet {

    @Override
    protected  synchronized void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行service方法.....");
        //获取文件路径
        String realPath=req.getServletContext().getRealPath("/WEB-INF/count");
        File file=new File(realPath);
        System.out.println(realPath);
        //创建properties对象
        Properties properties=new Properties();
        FileInputStream fileInputStream=new FileInputStream(file);
        //加载存数据的properties文件
        properties.load(fileInputStream);
        fileInputStream.close();

        System.out.println(properties);
        //获取之前的浏览量
        String s=properties.getProperty("count");
        //声明一个存浏览量的变量
        int num=0;
        try {
            //更改浏览量
           num=Integer.parseInt(s);
           num++;
        }catch (Exception e){
            e.printStackTrace();
        }
         //更新浏览量
        properties.setProperty("count", String.valueOf(num));
        FileOutputStream fileOutputStream=new FileOutputStream(realPath);
        //注释部分
        properties.store(fileOutputStream, "successful");
        System.out.println("当前浏览量"+num);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("初始化........");
    }
}

效果展示
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值