Hits Counter in a JSP Page
by, 07-07-2012 at 08:29 PM (3546 Views)
You have a website developed in JSP with different pages. The hit counter will help you the total number of users visit your any JSP page. In most cases, the hit counter is attached with “index.jsp” page. It is the main login page and most users will first visit this page before navigating to any other page on your application or website.
The following methods are used to implement hit counter. These methods are attached with you “Application” object.
The Application object is available in a JSP page and the Application object is created at JSP page initialization. It is destroyed when jspDestroy() method is called at the end of JSP page execution.
To set an attribute at application level, following piece of code is used:
The above setAttribute() method can be used to set the hit counter value and it is also used to reset the counter.Java Code:application.setAttribute(String key, Object Value);
Following piece of code is used to read the value.
Use the above 2 methods to read and increment the counter value whenever a user visit your JSP page.Java Code:application.getAttribute(String Key);