栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java获取不同网站访问量

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java获取不同网站访问量

以不同表单id为参数,区分不同网站

package com.example.datafile.controller;

import com.example.datafile.common.utils.RedisClientTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "获取访问量")
@RequestMapping("/counts")
public class CountServlet {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisClientTemplate redisClientTemplate;

    @GetMapping("")
    @ApiOperation("获取表单访问量")
    protected Integer doGet(HttpServletRequest request, HttpServletResponse response, @RequestParam long formId)
            throws ServletException, IOException {

        int totalCount = 1;//默认访问量为1
        //获取网站访问量
        Object count = request.getServletContext().getAttribute(String.valueOf(formId));
        //判断count是否为null,不为null表示曾经访问过,直接将count赋值给totalCount
        if (count != null) {
            totalCount = (int) count;
        }
        //访问次数累加
        request.getServletContext().setAttribute(String.valueOf(formId), totalCount + 1);
        redisClientTemplate.setToRedis("formId_" + String.valueOf(formId),String.valueOf(totalCount));
        return totalCount;
    }

}

参考:
https://blog.csdn.net/weixin_36151775/article/details/114671994

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458992.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号