JSP application to access objects that implement counter

<%@ page language="java" contentType="text/html; charset=UTF-8 "
    pageEncoding="UTF-8"%>
  <%@  page import="java.util.*"%> 
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<%
		
		if(application.getAttribute("counter") == null ){
			application.setAttribute("counter", "1");
		}else{
			String strnum = null;
			strnum = application.getAttribute("counter").toString();
			int iconut = 0;
			iconut = Integer.valueOf(strnum).intValue();
			iconut++;
			application.setAttribute("counter", Integer.toString(iconut));
			
		}

	%>
			You are <% = application.getAttribute ( "counter")%> visitors!  
		
</body>
</html>

  Page runs as follows:

 

After the refresh, the counter is incremented by 1 with the following results:

 

 

Replace the browser or replace client address will make it normal access value is incremented. If the Tomcat server is restarted, the counter will again start counting from 1.

 

Guess you like

Origin www.cnblogs.com/wwenwei/p/11649334.html