The code of jsp:
1 <body> 2 <h1> Web page counter </ h1> 3 <% 4 // The data for the first visit is empty 5 Object obj = application.getAttribute ( " count " ); 6 7 // The user's first visit 8 if (obj == null ) { 9 application.setAttribute ( " count " , 1 ); 10 } else { // non-first visit 11 Integer num = (Integer) obj; 12 application.setAttribute (" count " , ++ num); 13 14 } 15 16 %> 17 18 Welcome, you are the <% = application.getAttribute ( " count " )%> person who visited the changed page 19 </ body> 20 < / html>
Even if you change the browser or close the browser, our application is the only one, so the number of people who visit the page will continue to increase.
If the code application in jsp is replaced by session:
1 <h1> Web page counter </ h1> 2 <% 3 // The data for the first visit is empty 4 Object obj = session.getAttribute ( " count " ); 5 6 // The user's first visit 7 if (obj = = null ) { 8 session.setAttribute ( " count " , 1 ); 9 } else { // non-first visit 10 Integer num = (Integer) obj; 11 session.setAttribute ( " count ", ++ num); 12 13 } 14 15 %> 16 17 Welcome to you, you are the first person to visit <% = session.getAttribute ( " count " )%> 18 </ body> 19 </ html>
result:
As long as you get the ServletContext, it means you are globally unique
This is clear