`
yacole
  • 浏览: 238063 次
  • 性别: Icon_minigender_1
  • 来自: 浙江科技学院
社区版块
存档分类
最新评论

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

代码
  1. <HTML>    
  2. <HEAD>    
  3. <TITLE> New Document </TITLE>    
  4. <BODY BGCOLOR="#FFFFFF">    
  5. <%    
  6. application.setAttribute("counter",new Integer(0));    
  7. out.println(application.getAttribute("counter"));    
  8. %>    
  9. </BODY>    
  10. </HTML>    
  11. count.jsp    
  12. <HTML>    
  13. <HEAD>    
  14. <TITLE> New Document </TITLE>    
  15. </HEAD>    
  16. <BODY BGCOLOR="#FFFFFF">    
  17. <%    
  18. Integer i=(Integer)application.getAttribute("counter");    
  19. i=new Integer(i.intValue()+1);    
  20. application.setAttribute("counter",i);    
  21. out.println((Integer)application.getAttribute("counter"));    
  22. %>    
  23. </BODY>    
  24. </HTML>  
分享到:
评论
2 楼 zhuge 2009-03-06  
1 楼 zhuge 2009-03-06  

相关推荐

Global site tag (gtag.js) - Google Analytics