A simple website counter

9707 단어 website
When making a website, it is often necessary to count how many people have visited the website. The count should be based on whether it is a new call to judge whether it is a new user visiting the website. The application object can save the number of visitors.
About the application object: The application object is used to share data between multiple programs or multiple users. The application object used by the user is the same. This is different from the session. Once the server is started, the application object will be automatically created and kept forever Go down until the server shuts down and the application object disappears automatically. The following is to use the application object to implement a website counter, the code is as follows:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 3 <html>

 4   <body>

 5       <%!

 6         synchronized void count(){      // sychronized ,

 7                                         // .

 8             ServletContext application = ((HttpServlet)(this)).getServletContext();

 9             Integer count = (Integer)application.getAttribute("count");

10             

11             if(count==null){

12                 count = new Integer(1);

13                 application.setAttribute("count",count);

14             }else{

15                 count = new Integer(count.intValue()+1);

16                 application.setAttribute("count",count);

17             }

18         }      

19       %>

20       

21       <%

22           if(session.isNew()){        // .

23               count();

24               out.println(" "+application.getAttribute("count")+" ");

25           }

26       %>

27   </body>

28 </html>

좋은 웹페이지 즐겨찾기