JSP implements web page access statistics, and jsp implements access statistics

Source: Internet
Author: User

JSP implements web page access statistics, and jsp implements access statistics

I recently learned about jsp web page development in Jave EE and needed to collect statistics on Webpage access traffic. I didn't know how to implement it at the beginning. Then I asked my teacher, who answered me like this: to achieve web page access statistics, you can use the application object. You cannot use the seesion object. Because the session belongs to the same session, the data in the browser is no longer available after it is disabled, the application is in the same browser. As long as the application is in the same browser, the data is stored in the applicaiton object, which ensures data immutability. In fact, I understand all this, but I just don't know how to implement it using code in jsp. Later, I only had to go online to see if there were any specific solutions. After searching for a long time, I didn't have the answer I wanted. What I wanted to achieve was simple statistics, and I didn't implement more complex functions. Later, I found the answer here in CSDN. Here I will briefly summarize several methods for implementing web page access statistics:
1. Use the application object for statistics. The result is that the statistics are collected every time the page is accessed. But the effect is not very good, because the general statistics on the page traffic, refresh is not included in the statistics, this is the disadvantage.
The specific implementation is:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> 

2. to solve the problem above, another method is to use the application object and session object for statistics at the same time. The principle of this method is to access the browser from opening the browser to closing the browser, refreshing, returning, and other operations are not counted as one access. However, there are still some defects. When the jsp server is restarted, the data is also cleared.
The specific implementation is as follows:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> 

3. The third method is to store the statistical data in a local file, such as a txt file.
This is to avoid data loss after the server is restarted.
Create a class: JSPCount

Import java. io. bufferedReader; import java. io. file; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; public class JSPCount {// file Writing Method public static void write2File (String filename, long count) {try {PrintWriter out = new PrintWriter (new FileWriter (filename )); out. println (count); out. close ();} catch (IOException e) {// TODO: handle exception e. printStackTrace () ;}// Method for reading files public static long readFromFile (String filename) {File file = new File (filename); long count = 0; if (! File. exists () {try {file. createNewFile ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} write2File (filename, 0);} try {BufferedReader in = new BufferedReader (new FileReader (file); try {count = Long. parseLong (in. readLine ();} catch (NumberFormatException e) {// TODO: handle exception e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO: handle exception e. printStackTrace () ;}return count ;}}

Create a jsp file in the WebRoot directory: count. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> <% @ page import =" org. wwj. count. JSPCount "%> 

After the program is run, a count.txt text file will be generated for the corresponding webproject in the tomcat的web.

4. The fourth method is to save the access statistics, but it is not guaranteed that the page will not be automatically refreshed. Of course, there will always be solutions. The general solution is to combine the advantages of various solutions. The following describes how to collect Website access statistics by using session Object + application Object + txt text.

Import java. io. bufferedReader; import java. io. file; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; import javax. servlet. http. httpServlet; public class Counter extends HttpServlet {// Method for writing files public static void write2File (String filename, long count) {try {PrintWriter out = new PrintWriter (new FileWri Ter (filename); out. println (count); out. close ();} catch (IOException e) {// TODO: handle exception e. printStackTrace () ;}// Method for reading files public static long readFromFile (String filename) {File file = new File (filename); long count = 0; if (! File. exists () {try {file. createNewFile ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} write2File (filename, 0);} try {BufferedReader in = new BufferedReader (new FileReader (file); try {count = Long. parseLong (in. readLine ();} catch (NumberFormatException e) {// TODO: handle exception e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO: handle exception e. printStackTrace () ;}return count ;}}

Jsp file code:

<% @ Page import = "org. servlet. count. counter "%> <% @ page language =" java "import =" java. util. * "pageEncoding =" GB2312 "%> 

The above four methods are obtained after each improvement. If you want to achieve Website access statistics, of course, the last one is the best. Knowledge is not a one-step journey, and you need to constantly improve the problem, to obtain the final solution, of course, the last one is not necessarily the best. In terms of implementation strategies, it is also possible to use the database, however, I think that every time I access a website, I need to read and write databases, which reduces the efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.