application:提供了關于服務器版本,應用級初始化參數和應用內資源據對路徑的方式
application作用域:web容器的生命周期,即多用戶登錄時的數據可以同時進行訪問,如網站的訪問量(如果有數據需要在整個web應用程序中進行訪問的話使用application中的attribute方法即可)
訪問量代碼:
if (flag == true){
? ? Object o = application.getAttribute("count");//獲取訪問的次數
? ? if (o == null){ //判斷是否為空即為0的情況
? ? ? ? application.setAttribute("count",1);//當訪問次數為空時修改為1,存入application中
? ? }else {
? ? ? ? int count = Integer.parseInt(o.toString());//將獲取的object類型的訪問次數
? ? ? ? application.setAttribute("count",count + 1);//累加訪問的數量
? ? }
前臺顯示代碼為:
<p align="left">訪問量為:<%= application.getAttribute("count")%></p>
顯示效果為: