How to Design Page Hit Counter Using Servlet and Jsp

The following content is quoted fromhttp://wiki.jikexueyuan.com/project/jsp/hits-counter.html:

A click counter can know the number of visits to a particular page of the website. Assuming that people log on to the homepage for the first time, a click counter is usually set on the index.jsp page.

You can use the Application implicit object and related methods getAttribute() and setAttribute() to implement a click counter.

This object represents this JSP page through its entire life cycle. The JSP page is created when the object is initialized, and the object is also deleted when the JSP page is deleted by the jspDestroy() method.

The following is the syntax for setting variables at the application layer:

application.setAttribute(String Key, Object Value);

You can use the above method to set the click counter variable or reset the same variable. The next description is a method that reads the variables set by the previous method.

application.getAttribute(String Key);

Every time a user visits a web page, he can read the current value of the click counter, increase by 1 and set the click counter again for future use.

Example:

This example shows how to use JSP to count clicks on a specific page. If you want to count website hits, you will have to include the same code in all JSP pages.

            <%            @ page import            =            "            java.io.*,java.util.*            "            %>            <            html            >            <            head            >            <            title            >Applcation object in JSP</            title            >            </            head            >            <            body            >            <%            Integer                          hitsCount                        =                          (            Integer            )application.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            ;     }     application.setAttribute(            "            hitCounter            "            , hitsCount);                        %>            <            center            >            <            p            >Total number of visits:            <%            =                          hitsCount            %>            </            p            >            </            center            >            </            body            >            </            html            >          

Now put the above code in main.jsp and use the URL:http://localhost:8080/main.jspTo call this JSP. Whenever the page is refreshed, the click counter value that will be displayed will increase. You can try to use a different browser to visit the webpage, and you will find that the counter will increase for each click. The results displayed are as follows:

Counter reset

If you restart an application such as a web server, this will reset the application variables and the click counter will be reset to zero. In order to avoid this loss, you can use the following professional methods to achieve click counter:

  • Define a database table with a single count called hits. Set its value to 0.

  • For each click, read the table to get the value of the click volume.

  • Add 1 to the number of clicks to update the value in the table.

  • The new value of the click counter is displayed as the total page clicks.

  • If you want to calculate the number of clicks on all pages, implement the above logic for all pages.

Test project:https://github.com/easonjim/5_java_example/tree/master/jspbasics/test14

LeetCode 362. Design Hit Counter

The link to the original question is here:https://leetcode.com/problems/design-hit-counter/description/ topic: Design a hit counter which counts the number of hits received in the past 5 minutes. Each...

Leetcode 362: Design Hit Counter

Design a hit counter which counts the number of hits received in the past 5 minutes. Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made t...

[LC] 362. Design Hit Counter

Design a hit counter which counts the number of hits received in the past 5 minutes. Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made t...

leetcode362 - Design Hit Counter - medium

Design a hit counter which counts the number of hits received in the past 5 minutes. Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made t...

springboot-jsp hit jar problem

【Recap】I recently made a project, the project is springboot+jsp structure, but when the production environment is released, it needs to be marked as a jar package with maven, but the default configura...

JSP simple exercise - a simple counter

In JSP, program code written between "<%" and "%>" is a Java block. A JSP page can have multiple Java blocks. It is to be noted that variables declared in the Java program ar...

Website Counter -JSP Java SMD

In JSP, the program code written between "<%%>" is called the Java block, and the HTML statement can be inserted therein. There are multiple Java blocks in a JSP page. However, variabl...

[JSP] Write a webpage counter using Application

Write a web counter using Application Implement this application, use two methods below Application: Application.setttribute () and Application.getaTribute () code show as below: View Code...

How to Design Page Hit Counter Using Servlet and Jsp

Source: https://www.programmerall.com/article/60682626/

0 Response to "How to Design Page Hit Counter Using Servlet and Jsp"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel