jsp教程--application 的應用
在前一篇里我們講了在jsp 中使用session 來保存每個用戶的私有信息,但有時服務器需要管理面向整個應用的參數,使得每個客戶都能獲得同樣的參數值。那在jsp中應怎么辦呢?和session 一樣, jsp使用application 對象,操作的方法和session "times new roman""一樣。
其api 使用如下:
application .setattribute("item", itemvalue); //設置一個應用變量
integer i=(integer) application.getattribute("itemname"); // 得到//item
現以一個簡單統計在線人數的的例子來說明application的應用(這里不考慮離開的情況),init.jsp(初始化),count.jsp( 統計總人數并輸出)。
init.jsp
<html>
<head>
<title> new document </title>
<body bgcolor="#ffffff">
<%
application.setattribute("counter",new integer(0));
out.println(application.getattribute("counter"));
%>
</body>
</html>
count.jsp
<html>
<head>
<title> new document </title>
</head>
<body bgcolor="#ffffff">
<%
integer i=(integer)application.getattribute("counter");
i=new integer(i.intvalue()+1);
application.setattribute("counter",i);
out.println((integer)application.getattribute("counter"));
%>
</body>
</html>
新聞熱點
疑難解答