View RSS Feed


Hits Counter in a JSP Page

Rate this Entry
, 07-07-2012 at 07:29 PM (2465 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.

• setAttribute()
• getAttribute()

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:
Java Code:
application.setAttribute(String key, Object Value);
The above setAttribute() method can be used to set the hit counter value and it is also used to reset the counter.

Following piece of code is used to read the value.
Java Code:
application.getAttribute(String Key);
Use the above 2 methods to read and increment the counter value whenever a user visit your JSP page.

Submit "Hits Counter in a JSP Page" to Facebook Submit "Hits Counter in a JSP Page" to Digg Submit "Hits Counter in a JSP Page" to Submit "Hits Counter in a JSP Page" to StumbleUpon Submit "Hits Counter in a JSP Page" to Google

Tags: hits counter Add / Edit Tags