Description: JSP uses the application object to implement simple counting of websites
Code: Note that web development is a multi-threaded operation, so synchronization is required
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP</title> </head> <body> <% // Add a synchronization method to ensure +1 synchronized(application) { // Get the last count, note that the application returns the Object type so it needs to be converted String data = (String)application.getAttribute("count"); if (data == null) { application.setAttribute("count", "1"); // first visit } else { int i = Integer.parseInt(data); // Get the last data i++; // +1 application.setAttribute("count", Integer.toString(i)); // rewrite } } %> <%-- Use EL for input, you can also use application.getAttribute("count") input--%> <h2>You are the ${count} visitor! </h2> </body> </html>
Disadvantage: The count value will be +1 every time it is refreshed, so it needs to be improved
Improvement: Use the isNew method of the session object to determine whether it is a new user, if it is, +1, otherwise it will not be added
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP</title> </head> <body> <% // Join the session to determine whether it is a new user if (session.isNew()) { // Add a synchronization method to ensure +1 synchronized(application) { // Get the last count, note that the application returns the Object type so it needs to be converted String data = (String)application.getAttribute("count"); if (data == null) { application.setAttribute("count", "1"); // first visit } else { int i = Integer.parseInt(data); // Get the last data i++; // +1 application.setAttribute("count", Integer.toString(i)); // rewrite } } } %> <%-- Use EL for input, you can also use application.getAttribute("count") input--%> <h2>You are the ${count} visitor! </h2> </body> </html>Tip: There are still shortcomings, because the application object will disappear after restarting the server, so if you develop counters, you should use the method of saving to a file. When the user accesses and is a new user, +1 is added and the data is written to the file, so that As a result, even if the server is changed, it will not affect the previous visitor statistics.
Final improvements:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%-- import package--%> <%@ page import="java.io.* "%> <%@ page import="java.math.* "%> <%@ page import="java.util.* "%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP</title> </head> <body> <%-- BigInteger can be used if the amount of data is large --%> <%-- Define global count for counting--%> <%! BigInteger count = null; %> <%-- define global method--%> <%! // define file operation method public BigInteger getCount(File file) { BigInteger count = null; // The operation of the file will be abnormal, so it needs to be caught try{ if (file.exists()) { // must determine if the file exists Scanner scan = new Scanner(new FileInputStream(file)); // There is only one data in the file, so just if not while if (scan.hasNext()) { count = new BigInteger(scan.next()); // get data } scan.close(); // close the input stream } else{ count = new BigInteger("0"); // new save(file, count); // 保存 } } catch(Exception e){ e.printStackTrace (); } return count; // return value } // save document public void save(File file, BigInteger count) { try{ PrintStream out = null; // instantiate the print stream out = new PrintStream(new FileOutputStream(file)); out.print(count); // write data out.close(); // close the print stream } catch(Exception e){ e.printStackTrace (); } } %> <% // Get the real path of the virtual path and create a new count.txt file in the root directory String path = this.getServletContext().getRealPath("/") + "count.txt"; File file = new File(path); if (session.isNew()) { synchronized(this){ count = getCount(file); // get the value count = count.add(new BigInteger("1")); // count value + 1 System.out.println(count); save(file, count); // 保存 } } %> <%-- Consider the case of count==null--%> <h2>You are the <%=(count==null)? 0 : count%> visitor! </h2> </body> </html>Conclusion: The last improved version can be applied to any website, no matter how big the data is