Servlet Hits Counter
π
2026-06-19 | π Servlet
Often, you might be interested in knowing the total number of hits on a specific page of a website. Using a Servlet to calculate these hits is very simple because the lifecycle of a Servlet is controlled by the container in which it runs.
Here are the steps required to implement a simple web page hit counter based on the Servlet lifecycle:
* Initialize a global variable in the `init()` method.
* Increment the global variable 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 inside the `init()` method when the Servlet is initialized next time. This step is optional.
* If you want to count a page hit only once per session, use the `isNew()` method to check if the session has already hit the same page. This step is optional.
* You can display the total hits on the website by showing the value of the global counter. This step is optional.
Here, we assume that the web container will not be restarted. If it is restarted or the Servlet is destroyed, the counter will be reset.
## Example
This example demonstrates how to implement a simple web page hit counter:
```java
package com.tutorial.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class PageHitCounter
*/
@WebServlet("/PageHitCounter")
public class PageHitCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
private int hitCount;
public void init() {
// Reset the hit counter
hitCount = 0;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// Increment hitCount
hitCount++;
PrintWriter out = response.getWriter();
String title = "Total Hits";
String docType = " n";
out.println(docType +
"n" +
"
" + title + "n" +
"n" +
"" + title + "
n" +
"" + hitCount + "
n" +
"");
}
public void destroy() {
// This step is optional, but if needed, you can write the value of hitCount to a database
}
}
Now let us compile the above Servlet and create the following entry in the `web.xml` file:
```xml
PageHitCounter
com.tutorial.test.PageHitCounter
PageHitCounter
/TomcatTest/PageHitCounter
Now call this Servlet by accessing `http://localhost:8080/TomcatTest/PageHitCounter`. This will increment the counter value by 1 each time the page is refreshed, resulting in the following output:
## Total Hits
## 6
Often, you might be interested in knowing the total hits for the entire website. In a Servlet, this is also very simple, and we can achieve this using a Filter.
Here are the steps required to implement a simple website hit counter based on the Filter lifecycle:
* Initialize a global variable in the filter's `init()` method.
* Increment the global variable each time the `doFilter` method is called.
* If needed, you can use a database table to store the value of the global variable in the filter's `destroy()`. This value can be read inside the `init()` method when the filter is initialized next time. This step is optional.
Here, we assume that the web container will not be restarted. If it is restarted or the Servlet is destroyed, the hit counter will be reset.
## Example
This example demonstrates how to implement a simple website hit counter:
```java
// Import required java libraries
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{
// Reset the hit counter
hitCount = 0;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException {
// Increment the counter value
hitCount++;
// Print the counter
System.out.println("Site Visit Count: "+ hitCount );
// Pass request back down the filter chain
chain.doFilter(request,response);
}
public void destroy() {
// This step is optional, but if needed, you can write the value of hitCount to a database
}
}
Now let us compile the above Servlet and create the following entry in the `web.xml` file:
```xml
....
SiteHitCounter
SiteHitCounter
SiteHitCounter
/*
....
Now visit any page on the website, for example, `http://localhost:8080/`. This will increment the counter value by 1 each time any page is visited, and it will display the following message in the logs:
Site Visit Count: 1
Site Visit Count: 2
Site Visit Count: 3
Site Visit Count: 4
Site Visit Count: 5
..................