以不同表单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



