SoFunction
Updated on 2025-03-08

JS+JSP implements the static page access count through img tag call

This article describes the method of JS+JSP to realize the number of static page access statistics through img tag calls. Share it for your reference, as follows:

Test page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>test</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="">
  -->
 </head>
 <body>
 this is a test page.
<script type="text/javascript">("<img src=http://127.0.0.1:8080/EasyCMS/ border=0 width=0 height=0>");</script>
 </body>
</html>

Statistical procedures::

Assume that the deployment location is http://127.0.0.1:8080/EasyCMS/

&lt;%@ page language="java" import=".*" pageEncoding="UTF-8"%&gt;
&lt;%@page import=".*"%&gt;
&lt;%
String path="/opt/";
writeNumber((readNumber(path)+1),path);
%&gt;
&lt;%=readNumber(path) %&gt;
&lt;%!
  /**
    * Write digital content
    *
    * @param number
    * @param filename
    * @return
    */
  public boolean writeNumber(String number, String filename) {
    try {
      FileOutputStream fos = new FileOutputStream(filename);
      OutputStreamWriter writer = new OutputStreamWriter(fos);
      (number);
      ();
      ();
    } catch (IOException e) {
      ();
      return false;
    }
    return true;
  }
  /**
    * Read digital content
    *
    * @param filename
    * @return
    */
  public int readNumber(String filename) {
    int number = 0;
    try {
      File file = new File(filename);
      if (()) {
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String contents = ();
        if (contents != null &amp;&amp; () &gt; 0) {
          contents = ("[^0-9]", "");
          number = (contents);
        }
        ();
        ();
      }
    } catch (IOException e) {
      ();
    }
    return number;
  }
%&gt;

Basic ideas:
 
When accessing a static page, src is specified as the address of the access statistics through the img tag, and the img tag issues a request to the statistical program to realize statistics.
The statistical sample code uses files to record the number of accesses, and the actual project can record the database.
 
Key Code:

Copy the codeThe code is as follows:
<script type="text/javascript">("<img src=http://127.0.0.1:8080/EasyCMS/ border=0 width=0 height=0>");</script>

I hope this article will be helpful to everyone's JavaScript programming.