Servlets Hits Counter


Hit Counter for a Web Page

Many times you would be keen on knowing all out number of hits on a specific page of your site. It is extremely easy to tally these hits utilizing a servlet on the grounds that the existence cycle of a servlet is constrained by the compartment in which it runs.

Following are the means to be taken to execute a straightforward page hit counter which depends on Servlet Life Cycle −

  • Initialize a worldwide variable in init() method.

  • Increase worldwide variable each time either doGet() or doPost() technique is called.

  • If required, you can utilize a database table to store the estimation of worldwide variable in pulverize() strategy. This esteem can be perused inside init() strategy when servlet would be instated next time. This progression is optional.

  • If you need to tally just one of a kind page hits with-in a session then you can utilize isNew() technique to check if same page as of now have been hit with-in that session. This progression is optional.

  • You can show estimation of the worldwide counter to demonstrate all out number of hits on your site. This progression is additionally optional.

Here I'm accepting that the web compartment won't be restarted. On the off chance that it is restarted or servlet pulverized, the hit counter will be reset.

Example

This precedent tells the best way to actualize a basic page hit 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() { 

//Reset hit counter. 

hitCount = 0; 

} 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set reaction content type 

response.setContentType("text/html"); 

//This technique executes at whatever point the servlet is hit  

//increase hitCount  

hitCount++; 

PrintWriter out = response.getWriter(); 

String title = "Total Number of Hits"; 

String docType = "<!doctype html open \"-/w3c//dtd html 4.0 " + "transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n" + 

"<body bgcolor = \"#f0f0f0\">\n" + 

"<h1 adjust = \"center\">" + title + "</h1>\n" + 

"<h2 adjust = \"center\">" + hitCount + "</h2>\n" + 

"</body> 

</html>" 

); 

} 

public void destroy() { 

//This is discretionary advance yet in the event that you like you 

//can compose hitCount esteem in your database. 

} 

} 

Now let us gather above servlet and make following passages 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 utilizing URL http://localhost:8080/PageHitCounter. This would build counter by one each time this page gets invigorated and it would show following outcome −

Total Number of Hits

6

Hit Counter for a Website:

Many times you would be keen on knowing all out number of hits on your entire site. This is additionally exceptionally straightforward in Servlet and we can accomplish this utilizing filters.

Following are the means to be taken to actualize a basic site hit counter which depends on Filter Life Cycle −

  • Initialize a worldwide variable in init() strategy for a filter.

  • Increase worldwide variable each time doFilter technique is called.

  • If required, you can utilize a database table to store the estimation of worldwide variable in decimate() technique for channel. This esteem can be perused inside init() technique when channel would be instated next time. This progression is optional.

Here I'm accepting that the web holder won't be restarted. In the event that it is restarted or servlet annihilated, the hit counter will be reset.

Example

This precedent tells the best way to execute a straightforward site hit counter −

//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 hit counter. 

hitCount = 0; 

} 

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 

throws java.io.IOException, ServletException { 

//increment counter by one 

hitCount++; 

//Print the counter. 

System.out.println("Site visits check :"+ hitCount ); 

//Pass ask for withdraw the channel chain 

chain.doFilter(request,response); 

} 

public void destroy() { 

//This is discretionary advance however in the event that you like you 

//can compose hitCount esteem in your database. 

} 

} 

Now let us incorporate the above servlet and make the accompanying passages 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> 

</channel mapping> 

....

Now call any URL like URL http://localhost:8080/. This would expand counter by one each time any page gets a hit and it would show following message in the log −

Site visits check : 1 

Site visits check : 2 

Site visits check : 3 

Site visits check : 4 

Site visits check : 5 

.................. 







© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3