Servlet click counter
Web Page Click Counter
Many times, you may be interested in knowing the total number of clicks on a specific page of your website. Counting these hits using servlets is very simple because the life cycle of a servlet is controlled by the container in which it runs.
The following are the steps needed to implement a simple web page click counter based on the Servlet life cycle:
Initialize a global variable in the init() method.
Increments global variables each time the doGet() or doPost() method is called.
If needed, you can use a database table to store the value of the global variable in destroy(). This value can be read within the init() method the next time the Servlet is initialized. This step is optional.
If you only want to count one page click per session, then use the isNew() method to check if the same page has been clicked by that session. This step is optional.
You can display the total number of clicks on a page on your website by displaying the value of a global counter. This step is optional.
Here we assume that the web container will not be able to restart. If there is a restart or the Servlet is destroyed, the counter will be reset.
Example
This example demonstrates how to implement a simple web page click counter:
import java.io.*; import java.sql.Date; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class PageHitCounter extends HttpServlet{ private int hitCount; public void init() { // 重置点击计数器 hitCount = 0; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html"); // 该方法在 Servlet 被点击时执行 // 增加 hitCount hitCount++; PrintWriter out = response.getWriter(); String title = "总点击量"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<h2 align=\"center\">" + hitCount + "</h2>\n" + "</body></html>"); } public void destroy() { // 这一步是可选的,但是如果需要,您可以把 hitCount 的值写入到数据库 } }
Now let us compile the above Servlet and create it in the web.xml file Following entry:
.... <servlet> <servlet-name>PageHitCounter</servlet-name> <servlet-class>PageHitCounter</servlet-class> </servlet> <servlet-mapping> <servlet-name>PageHitCounter</servlet-name> <url-pattern>/PageHitCounter</url-pattern> </servlet-mapping> ....
Now call this Servlet by accessing the URL http://localhost:8080/PageHitCounter. This will increase the counter value by 1 every time the page is refreshed, and the result will be as follows:
Total Clicks6 |
Website Click Counter
Many times, you may be interested in knowing the total number of hits for your entire website. In Servlets this is also very simple, we can do this using filters.
The following are the steps you need to take to implement a simple website click counter based on the filter life cycle:
Initialize a global in the filter's init() method variable.
Every time the doFilter method is called, the global variable is increased.
If desired, you can use a database table to store the value of the global variable in the filter's destroy(). This value can be read within the init() method the next time the filter is initialized. This step is optional.
Here we assume that the web container will not be able to restart. If there is a restart or the Servlet is destroyed, the click counter will be reset.
Example
This example demonstrates how to implement a simple website click counter:
// 导入必需的 java 库 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class SiteHitCounter implements Filter{ private int hitCount; public void init(FilterConfig config) throws ServletException{ // 重置点击计数器 hitCount = 0; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // 把计数器的值增加 1 hitCount++; // 输出计数器 System.out.println("网站访问统计:"+ hitCount ); // 把请求传回到过滤器链 chain.doFilter(request,response); } public void destroy() { // 这一步是可选的,但是如果需要,您可以把 hitCount 的值写入到数据库 } }
Now let us compile the above Servlet and create it in the web.xml file The following entries:
.... <filter> <filter-name>SiteHitCounter</filter-name> <filter-class>SiteHitCounter</filter-class> </filter> <filter-mapping> <filter-name>SiteHitCounter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ....
Now visit any page of the website, such as http://localhost:8080/. This will increment the counter by 1 every time any page is clicked, which will display the following message in the log:
网站访问统计: 1 网站访问统计: 2 网站访问统计: 3 网站访问统计: 4 网站访问统计: 5 ..................