Last Updated on 28 May 2019   |   Print Email
In this tutorial, I will guide you how to implement the hit counter feature that displays total number of pageviews and number of online visitors for a Java web application – this kind of information is displayed at the end of every web page like this:
Total Pageviews: 123,456 – Online Visitors: 45
When a user visits a webpage, a pageview is counted and accumulated to the total pageviews. To count the pageviews, we use a Java Servlet Filter that intercepts all requests coming to the web application, and increase the pageview number by 1 for every request. The following picture illustrates this technique:The benefit of using filter mechanism is that, it doesn’t alter any pages nor existing code – thus easy to implement. The pseudocode to implement this filter would be like this:
class Filter {
public void doFilter() {
// increase pageviews by 1
// allow the request to reach the destination
}
}
The pageviews (hit count) information should be stored permanently because the web application or the server can be shut down or restarted. For the sake of simplicity, I use a text file to store the number of pageviews. So the workflow in the filter would be updated like this:
class Filter {
public void doFilter() {
// read current pageviews from file
// increase pageviews by 1
// update pageviews to file
// allow the request to reach the destination
// display pageviews at the end of the page
}
}
And to count the number of online visitors, we can take advantage of a HttpSessionListenerthat is triggered when a session event happens (session created and session destroyed). The pseudo code would be like this:
class HttpSessionListener {
public void sessionCreated() {
// increase online users by 1
}
public void sessionDestroyed() {
// decrease online users by 1
}
}
When a user visits the website, a new session is created and the session is counted as one online user. After the user leaves the website for sometimes (after a timeout period), the session is destroyed – hence the number of online users should be decreased by one. Notice the online users number is a just a rough estimation – not exact one.Here’s the real implementation code of a HttpSessionListener that is used to track the number of online users:
As you can see, the number of online users is stored as an attribute in the servlet context (application scope), so it can be used by the hit counter filter to display the number of online visitors in webpages. Here’s code of the filter class that is used to count the number of pageviews to the site:
/**
* Copyright by www.codejava.net
*/
package net.codejava;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/*")
public class HitCounterFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ServletContext context = request.getServletContext();
String realWebAppPath = context.getRealPath("");
String hitFilePath = realWebAppPath.concat("hit.txt");
File hitFile = new File(hitFilePath);
long currentHit = readHitCounterFromFile(hitFile);
updateHitCounterFile(++currentHit, hitFile);
CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
int onlineUsers = (Integer) context.getAttribute(UserSessionListener.ONLINE_USERS);
displayHitCounter(wrapper, response, currentHit, onlineUsers);
}
// see code of other methods below...
}
Note that the pageviews information is stored in the hit.txt file which is created under the web application’s root directory:
And to display the hit counter information (pageviews and online users) at the end of every webpages, we need to code a response wrapper class as follows:
package net.codejava;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class CharResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter writer;
public CharResponseWrapper(HttpServletResponse response) {
super(response);
writer = new CharArrayWriter();
}
public PrintWriter getWriter() {
return new PrintWriter(writer);
}
public String toString() {
return writer.toString();
}
}
Note that to allow the filter to alter the response, we need to invoke the doFilter() method on the FilterChainlike this:
CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
Then in this filter, the number of online users is read from an attribute in the servlet context using the following statement:
int onlineUsers = (Integer) context.getAttribute(UserSessionListener.ONLINE_USERS);
And code of the method that puts a piece of HTML code at the end of every webpage to display the hit counter information is as follows:
To understand more about how to modify the response using Java filter, read this article.For reference, you can download the full project’s code in the Attachments section below. When running the web application, you would see the result like this:That’s the solution to implement the hit counter feature to display the total pageviews and current online users for a website developed in Java. You can use the code in this tutorial for your project. And I recommend you to take this course to learn developing a complete e-commerce website with Java, Servlet, JSP and Hibernate framework.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments