English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Sometimes we need to know how many times a page has been accessed, and at this point we need to add a page counter to the page. Page access statistics are generally accumulated on the page visit count when the user first loads the page.
To implement a counter, you can use the application implicit object and related methods getAttribute() and setAttribute() to achieve this.
This object represents the entire lifecycle of a JSP page. It is created when the JSP page is initialized and is deleted when the jspDestroy() method of the JSP page is called.
The syntax for creating a variable in the application is as follows:
application.setAttribute(String Key, Object Value);
You can use the above method to set a counter variable and update the value of the variable. The method to read the variable is as follows:
application.getAttribute(String Key);
You can read the current value of the counter and increment it each time the page is accessed.1and then reset it, so that the new value will be displayed on the page when the next user visits.
This example will introduce how to use JSP to calculate the total number of visits to a specific page. If you want to calculate the total number of clicks on the pages used on your website, you must place this code on all JSP pages.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*"%> <html> <html> <head> <title>Page View Statistics</title> </head> <body> <% Integer hitsCount = (Integer)application.getAttribute("hitCounter"); if( hitsCount == null || hitsCount == 0 ) { /* First visit */ out.println("Welcome to the Basic Tutorial Website!"); hitsCount = 1; } else { /* Return Visit Value */ out.println("Welcome back to the Basic Tutorial website!"); hitsCount += 1; } application.setAttribute("hitCounter", hitsCount); %> <p>Page visit count: <%= hitsCount %></p>/p> </body> </html>
Now we will place the above code in the main.jsp file and accesshttp://localhost:8080/testjsp/main.jspFile. You will see that the page generates a counter, and it will change each time we refresh the page (increment each refresh)1).
You can also visit through different browsers, and the counter will increase after each visit1. As shown below:
Using the above method, the counter will be reset to 0 after the web server restarts, which means the data before will be lost. You can solve this problem in the following ways:
Define a data table count in the database for counting web page visits, with the field hitcount, where hitcount has a default value of 0 and the statistics are written to the data table.
We read the hitcount field in the table each time a visit occurs.
Increment the hitcount each time a visit occurs 1.
Display the new hitcount value as the page visit count on the page.
If you need to count the number of visits to each page, you can use the above logic to add the code to all pages.