Implementation of Web site count JSP

Source: Internet
Author: User

In some blogs or forums, there are often some number of visits, which is the usual website counter. The following 3 issues need to be noted for website counter development:

1. The number of visitors to the website is very large, so it must be represented by big integers;

2, each user in the first visit needs to count, repeat refresh page should not repeat count;

3, the site access to the value of the changes in the number of multi-threaded operations, need to synchronize operations.

The JSP code for the simulated Web site counters that you write is as follows:

  1. <%@ page contenttype= "text/html" pageencoding= "GBK"%>

  2. <%@ page import= "java.io.*"%> <%--you must import the Java.io package because you want to use IO operations--%>

  3. <%@ page import= "java.util.*"%> <%--scanner defined java.util in--%>

  4. <%@ page import= "java.math.*"%> <%--biginteger defined in Java.math--%>

  5. <title> website Counters </title>

  6. <body>

  7. <%!

  8. BigInteger count = null;

  9. %>

  10. <%!//the following methods in order to save trouble, directly in the method to deal with the exception, and the actual to be handed over to the call office processing

  11. Public BigInteger Load (file file) {//Read count file

  12. BigInteger count = null; Read the accepted data

  13. try {

  14. if (file.exists ()) {

  15. Scanner scan = null;

  16. Scan = new Scanner (new FileInputStream (file)); Read from File

  17. if (Scan.hasnext ()) {

  18. Count = new BigInteger (Scan.next ()); Put the content in the BigInteger

  19. }

  20. Scan.close (); Close the input stream

  21. } else {

  22. Count = new BigInteger ("0"); First time visit

  23. Save (File,count);

  24. }

  25. } catch (Exception e) {

  26. E.printstacktrace ();

  27. }

  28. return count; Returns the data after reading

  29. }

  30. public void Save (File File,biginteger count) {

  31. try {

  32. PrintStream PS = null; Defining an output Stream object

  33. PS = new PrintStream (new FileOutputStream (file)); Print Stream Object

  34. Ps.println (count);

  35. Ps.close ();

  36. } catch (Exception e) {

  37. E.printstacktrace ();

  38. }

  39. }

  40. %>

  41. <%

  42. String fileName = This.getservletcontext (). Getrealpath ("/") + "count.txt"; File path

  43. File file= new file (FileName);

  44. if (Session.isnew ()) {

  45. Synchronized (this) {

  46. Count = load (file);

  47. Count = Count.add (New BigInteger ("1")); Self-increment operation

  48. Save (File,count);

  49. }

  50. }

  51. %>

  52. </body>

Copy Code

The result of this code is:


When the first visit to this page shows "You are the 1th guest", the Refresh page appears unchanged, as shown in;

650) this.width=650; "id=" aimg_p8978 "class=" Zoom "border=" 0 "src=" http://img.blog.csdn.net/20150413175337654? watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvutm4oti4mtu0mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" 212 "/>


Close the browser when you open this page with a new browser, "You are the 2nd visitor", as shown in:

650) this.width=650; "id=" Aimg_i58z8 "class=" Zoom "border=" 0 "src=" http://img.blog.csdn.net/20150413175014199? watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvutm4oti4mtu0mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" 212 "/>

More Java learning, Java data http://techfoxbbs.com


Implementation of Web site count JSP

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.