jsp教程--application 的應(yīng)用
在前一篇里我們講了在jsp 中使用session 來保存每個(gè)用戶的私有信息,但有時(shí)服務(wù)器需要管理面向整個(gè)應(yīng)用的參數(shù),使得每個(gè)客戶都能獲得同樣的參數(shù)值。那在jsp中應(yīng)怎么辦呢?和session 一樣, jsp使用application 對(duì)象,操作的方法和session "times new roman""一樣。
其api 使用如下:
application .setattribute("item", itemvalue); //設(shè)置一個(gè)應(yīng)用變量
integer i=(integer) application.getattribute("itemname"); // 得到//item
現(xiàn)以一個(gè)簡(jiǎn)單統(tǒng)計(jì)在線人數(shù)的的例子來說明application的應(yīng)用(這里不考慮離開的情況),init.jsp(初始化),count.jsp( 統(tǒng)計(jì)總?cè)藬?shù)并輸出)。
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>
新聞熱點(diǎn)
疑難解答
圖片精選