利用JSP内置的Application对象实现的网站引用计数

时间:2022-07-22
本文章向大家介绍利用JSP内置的Application对象实现的网站引用计数,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

源代码:

<%@ page import="java.io.*,java.util.*" %>

<html>
<head>
<title>Applcation object in JSP</title>
</head>
<body>
<%     
Integer hitsCount = (Integer)application.getAttribute("hitCounter");     
if( hitsCount ==null || hitsCount == 0 ){             
    out.println("Welcome to my website!");        
    hitsCount = 1;     
}else{           
    out.println("Welcome back to my website!");        
    hitsCount += 1;     
}     
    application.setAttribute("hitCounter", hitsCount); 
%>
<center>
<p>Total number of visits: <%= hitsCount%></p>
</center>
</body>
</html>

每次刷新页面后,能观察到计数器依次加一。