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>
 
新闻热点
疑难解答