Spring Boot Starter (12) To achieve page access statistics function

Source: Internet
Author: User

?? In the daily use of the site, often encounter page visits (or the number of visitors) statistics. So how do you implement this feature in spring boot?
?? Our idea is relatively simple, that is to store the traffic in a certain place, to use the time to take out, the location can be stored in the database or other files. In this example, the TXT file is used, and we record the access data in the D-drive count.txt file.
?? The following direct start of this project. The complete structure of the entire project is as follows:

We only need to modify the red line of the three files, where the Build.gradle code is as follows:

buildscript {    ext {        springBootVersion = ‘2.0.3.RELEASE‘    }    repositories {        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")    }}apply plugin: ‘java‘apply plugin: ‘eclipse‘apply plugin: ‘org.springframework.boot‘apply plugin: ‘io.spring.dependency-management‘group = ‘com.example‘version = ‘0.0.1-SNAPSHOT‘sourceCompatibility = 1.8repositories {    mavenCentral()}dependencies {    compile(‘org.springframework.boot:spring-boot-starter-web‘)    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf    compile group: ‘org.springframework.boot‘, name: ‘spring-boot-starter-thymeleaf‘, version: ‘2.0.1.RELEASE‘}

View file (template) index. The code for HTML is as follows:

<!DOCTYPE html>

The code for the controller file Visitcontroller.java file is as follows:

Package Com.example.visit.controller;import Java.io.*;import Java.util.map;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.getmapping;@ Controllerpublic class Visitcontroller {@GetMapping ("/index") public String Index (MAP <string, object> map) { Gets the access amount information String Txtfilepath = "D://count.txt"; Long count = Get_visit_count (Txtfilepath); System.out.println (count); Map.put ("Count", count); Background parameters passed to the front end return "index"; }/* * Gets the number in the TXT file, that is, the previous traffic * passed in Parameter: string: Txtfilepath, absolute path to file */public static Long Get_visit_count (Strin G Txtfilepath) {try {//Read file (character stream) BufferedReader in = new BufferedReader (New Inputstreamread ER (new FileInputStream (Txtfilepath), "UTF-8")); Loop read data String str = NULL; StringBuffer content = new StringBuffer (); while ((str = in.readline ()) = null) {Content.append (sTR); }//Close stream in.close (); SYSTEM.OUT.PRINTLN (content); Parse gets the data Long count = long.valueof (content.tostring ()); Count + +; Traffic plus 1//write the appropriate file BufferedWriter out = new BufferedWriter (new OutputStreamWriter (New Fileoutputstre AM (Txtfilepath), "UTF-8"); Out.write (String.valueof (count)); Clear Cache Out.flush (); Close flow out.close (); return count; } catch (Exception e) {e.printstacktrace (); return 0L; } }}

This completes the configuration of the entire project, and finally, we write the number 0 in the count.txt in the D drive as the initial traffic.
?? Run the Spring boot project and enter Localhost:8080/index in the browser to display the following pages:

As soon as the page is loaded, the display page is accessed 1 times. When we have this also loaded 10 times, it is shown as follows:

This allows us to use spring boot to achieve the statistical function of page traffic.

?? This share to the end, welcome to Exchange ~ ~

Note: I have now opened two public number: because Python (number: Python_math) and easy to learn the Python crawler (number: Easy_web_scrape), welcome to the attention OH ~ ~

Spring Boot Starter (12) To achieve page access statistics function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.