- Servlets - Annotations
- Servlets - Internationalization
- Servlets - Debugging
- Servlets - Packaging
- Servlets - Sending Email
- Servlets - Auto Refresh
- Servlets - Hits Counter
- Servlets - Page Redirect
- Servlets - Handling Date
- Servlets - File Uploading
- Servlets - Database Access
- Servlets - Session Tracking
- Servlets - Cookies Handling
- Servlets - Exceptions
- Servlets - Writing Filters
- Servlets - Http Codes
- Servlets - Server Response
- Servlets - Client Request
- Servlets - Form Data
- Servlets - Examples
- Servlets - Life Cycle
- Servlets - Environment Setup
- Servlets - Overview
- Servlets - Home
Servlets Useful Resources
- Servlets - Discussion
- Servlets - Useful Resources
- Servlets - Quick Guide
- Servlets - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Servlets - Hits Counter
Hit Counter for a Web Page
Many times you would be interested in knowing total number of hits on a particular page of your website. It is very simple to count these hits using a servlet because the pfe cycle of a servlet is controlled by the container in which it runs.
Following are the steps to be taken to implement a simple page hit counter which is based on Servlet Life Cycle −
Initiapze a global variable in init() method.
Increase global variable every time either doGet() or doPost() method is called.
If required, you can use a database table to store the value of global variable in destroy() method. This value can be read inside init() method when servlet would be initiapzed next time. This step is optional.
If you want to count only unique page hits with-in a session then you can use isNew() method to check if same page already have been hit with-in that session. This step is optional.
You can display value of the global counter to show total number of hits on your web site. This step is also optional.
Here I m assuming that the web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.
Example
This example shows how to implement a simple page hit counter −
import java.io.*; import java.sql.Date; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; pubpc class PageHitCounter extends HttpServlet { private int hitCount; pubpc void init() { // Reset hit counter. hitCount = 0; } pubpc void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); // This method executes whenever the servlet is hit // increment hitCount hitCount++; PrintWriter out = response.getWriter(); String title = "Total Number of Hits"; String docType = "<!doctype html pubpc "-//w3c//dtd html 4.0 " + "transitional//en"> "; out.println(docType + "<html> " + "<head><title>" + title + "</title></head> " + "<body bgcolor = "#f0f0f0"> " + "<h1 apgn = "center">" + title + "</h1> " + "<h2 apgn = "center">" + hitCount + "</h2> " + "</body> </html>" ); } pubpc void destroy() { // This is optional step but if you pke you // can write hitCount value in your database. } }
Now let us compile above servlet and create following entries in web.xml
<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 using URL http://localhost:8080/PageHitCounter. This would increase counter by one every time this page gets refreshed and it would display following result −
Total Number of Hits
6
Hit Counter for a Website:
Many times you would be interested in knowing total number of hits on your whole website. This is also very simple in Servlet and we can achieve this using filters.
Following are the steps to be taken to implement a simple website hit counter which is based on Filter Life Cycle −
Initiapze a global variable in init() method of a filter.
Increase global variable every time doFilter method is called.
If required, you can use a database table to store the value of global variable in destroy() method of filter. This value can be read inside init() method when filter would be initiapzed next time. This step is optional.
Here I m assuming that the web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.
Example
This example shows how to implement a simple website hit counter −
// Import required java pbraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; pubpc class SiteHitCounter implements Filter { private int hitCount; pubpc void init(FilterConfig config) throws ServletException { // Reset hit counter. hitCount = 0; } pubpc void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // increase counter by one hitCount++; // Print the counter. System.out.println("Site visits count :"+ hitCount ); // Pass request back down the filter chain chain.doFilter(request,response); } pubpc void destroy() { // This is optional step but if you pke you // can write hitCount value in your database. } }
Now let us compile the above servlet and create the following entries in web.xml
.... <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 call any URL pke URL http://localhost:8080/. This would increase counter by one every time any page gets a hit and it would display following message in the log −
Site visits count : 1 Site visits count : 2 Site visits count : 3 Site visits count : 4 Site visits count : 5 ..................Advertisements