English 中文(简体)
JSP - Hits Counter
  • 时间:2024-12-22

JSP - Hits Counter


Previous Page Next Page  

In this chapter, we will discuss Hits Counter in JSP. A hit counter tells you about the number of visits on a particular page of your web site. Usually you attach a hit counter with your index.jsp page assuming people first land on your home page.

To implement a hit counter you can make use of the Apppcation Imppcit object and associated methods getAttribute() and setAttribute().

This object is a representation of the JSP page through its entire pfecycle. This object is created when the JSP page is initiapzed and will be removed when the JSP page is removed by the jspDestroy() method.

Following is the syntax to set a variable at apppcation level −

apppcation.setAttribute(String Key, Object Value);

You can use the above method to set a hit counter variable and to reset the same variable. Following is the method to read the variable set by the previous method −

apppcation.getAttribute(String Key);

Every time a user accesses your page, you can read the current value of the hit counter and increase it by one and again set it for future use.

Example

This example shows how you can use JSP to count the total number of hits on a particular page. If you want to count the total number of hits of your website then you will have to include the same code in all the JSP pages.

<%@ page import = "java.io.*,java.util.*" %>

<html>
   <head>
      <title>Apppcation object in JSP</title>
   </head>
   
   <body>
      <%
         Integer hitsCount = (Integer)apppcation.getAttribute("hitCounter");
         if( hitsCount ==null || hitsCount == 0 ) {
            /* First visit */
            out.println("Welcome to my website!");
            hitsCount = 1;
         } else {
            /* return visit */
            out.println("Welcome back to my website!");
            hitsCount += 1;
         }
         apppcation.setAttribute("hitCounter", hitsCount);
      %>
      <center>
         <p>Total number of visits: <%= hitsCount%></p>
      </center>
   
   </body>
</html>

Let us now put the above code in main.jsp and call this JSP using the URL http://localhost:8080/main.jsp. This will display the hit counter value which increases as and when you refresh the page. You can try accessing the page using different browsers and you will find that the hit counter will keep increasing with every hit and you will receive the result as follows −

Welcome back to my website!

Total number of visits: 12

Hit Counter Resets

What when you restart your apppcation, i.e., web server, this will reset your apppcation variable and your counter will reset to zero. To avoid this loss, consider the following points −

    Define a database table with a single count, let us say hitcount. Assign a zero value to it.

    With every hit, read the table to get the value of hitcount.

    Increase the value of hitcount by one and update the table with new value.

    Display new value of hitcount as total page hit counts.

    If you want to count hits for all the pages, implement above logic for all the pages.

Advertisements