JSP - Hits Counter
2015-09-01 14:53
645 查看
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
To implement a hit counter you can make use of Application Implicit object and associated methods
This object is a representation of the JSP page through its entire lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the
Following is the syntax to set a variable at application level:
You can use above method to set a hit counter variable and to reset the same variable. Following is the method to read the variable set by previous method:
Every time a use access your page, you can read current value of hit counter and increase it by one and again set it for future use.
Now let us put above code in main.jsp and call this JSP using URL http://localhost:8080/main.jsp. This would display hit counter value which would increase every time when you refresh the page. You can try to access the page using different browsers and you will find that hit counter will keep increasing with every hit and would display result something as follows:
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.
index.jsppage assuming people first land on your home page.
To implement a hit counter you can make use of Application Implicit object and associated methods
getAttribute()and
setAttribute().
This object is a representation of the JSP page through its entire lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the
jspDestroy()method.
Following is the syntax to set a variable at application level:
application.setAttribute(String Key, Object Value);
You can use above method to set a hit counter variable and to reset the same variable. Following is the method to read the variable set by previous method:
application.getAttribute(String Key);
Every time a use access your page, you can read current value of 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 total number of hits on a particular page. If you want to count total number of hits of your website then you would have to include same code in all the 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 let us put above code in main.jsp and call this JSP using URL http://localhost:8080/main.jsp. This would display hit counter value which would increase every time when you refresh the page. You can try to access the page using different browsers and you will find that hit counter will keep increasing with every hit and would display result something as follows:
Welcome back to my website! Total number of visits: 12
Hit Counter Resets:
What about if you re-start your application ie. web server, this will reset your application variable and your counter will reset to zero. To avoid this loss, you can implement your counter in professional way which is as follows: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.
相关文章推荐
- [LeetCode][JavaScript]Integer to English Words
- JSP Tutorial
- JSP - Include Directive
- js 计算两个日期之间的月数
- js实现点击向下展开的下拉菜单效果代码
- Jsp里中文乱码的解决办法
- javascript的正则表达式
- freemarker从JSON中取值
- 关于使用tpl模板如何从模板中取数据放入js中
- js数据类型
- javascript实现父子页面数据传递
- js 计算两个日期之间的周数
- js标点符号全局匹配
- js核心--------作用域链了解
- js 数组容易弄混的那些方法
- JS实现自动倒计时
- js获取url参数
- Javascript:谈谈JS的全局变量跟局部变量
- 获取服务器端JSON数据
- 寄生组合式继承——《Javascript 高级程序设计》6.3.6