application对象
站点所有的用户公用一个application对象,当站点服务器开启的时候,application就被创建,直到网站关闭。利用application这一特性,可以方便地创建聊天室和网站计数器等常用站点应用程序。 1 application的自定义属性可以为application对象添加属性,application对象常用的方法有两个: (1)public void setAttribute(String key, Object obj),将对象obj添加到application对象中,并为添加的对象添加一个索引关键字key。 (2)public Object getAttribute(String key),获取application对象中含有关键字key的对象。由于任何对象都可以添加到application中,因此用此方法取回对象的时候,需要强制转化为原来的类型。 application的自定义属性如程序5-19.jsp所示。 案例名称:自定义属性 程序名称:5-19.jsp <%@ page contentType="text/html;charset=GBK" %> <% String str = "你好"; application.setAttribute("greeting",str); %> <% String strBack = (String)application.getAttribute("greeting"); out.print(strBack); %> 程序首先对application的一个属性进行赋值,然后又将它取出来输出到浏览器上,程序显示的结果如图5-20所示。 图5-20 自定义属性 执行完后,该对象就被保存在服务器上。执行程序5-20.jsp时依然可以输出原先保存的值。 案例名称:自定义属性 程序名称:5-20.jsp <%@ page contentType="text/html;charset=GBK" %> <% String strBack = (String)application.getAttribute("greeting"); out.print(strBack); %> 虽然在该程序没有赋值,但是依然可以输出,因为5-19.jsp文件已经给application赋值,如图5-21所示。 图5-21 读取application属性的值 application变量不会因为某一个甚至全部用户离开而消失,一旦建立application变量,那么它就一直存在到网站关闭或者这个application对象被卸载,经常可能是几周或者几个月。 2 实现聊天室聊天室允许多用户实时进行信息交流,所有用户可以看到彼此的信息,这与application对象的特点正好符合,所以可以方便地利用application实现聊天室,如程序5-21.jsp所示。 案例名称:简易聊天室 程序名称:5-21.jsp <%@ page contentType="text/html;charset=GBK" %> <HTML> <BODY> <% if(application.getAttribute("chat")!=null){ if(request.getParameter("mywords")!=null){ String mywords = request.getParameter("mywords"); mywords = (String)application.getAttribute("chat") + "<br>" + mywords; application.setAttribute("chat", mywords); out.print((String)application.getAttribute("chat")); } } %> <FORM ACTION="5-21.jsp" METHOD="get"> <INPUT TYPE="TEXT" SIZE="30" NAME="mywords" VALUE="I LIKE CHAT"> <INPUT TYPE="SUBMIT" name="submit" VALUE="提交"> </FORM> </BODY> </HTML> 这时就可邀请一个朋友进入聊天室,虽然比较简易,不过已经实现了聊天室的功能,执行的结果如图5-22所示。 图5-22 简易聊天室 案例5-3:网页计数器 网页计数器是application 对象的又一个用途,因为application是所有的用户共有的,所以可以存储计数器的值,当有新用户访问网页时自动增加计数器的值,如程序CountV1.jsp所示。 案例名称:网页计数器版本一 程序名称:CountV1.jsp <%@ page contentType="text/html;charset=GB2312" %> <HTML> <BODY> <% Integer number=(Integer)application.getAttribute("Count"); if(number==null) { number=new Integer(1); application.setAttribute("Count",number); } else { number=new Integer(number.intValue() + 1); application.setAttribute("Count",number); } %> 您是第<%=(Integer)application.getAttribute("Count")%> 个访问本站的客户。 </BODY> </HTML> 程序显示结果如图5-23所示。 图5-23 网页计数器 一般网站的计数器都是图形界面,这个计数器也可以变成具有图形界面的计数器,如程序CountV2.jsp所示。 案例名称:网页计数器版本二 程序名称:CountV2.jsp <%@ page contentType="text/html;charset=GB2312" %> <HTML><BODY> <%! String G(Integer counter) { String S, myimage; myimage = ""; S = counter.toString() ; for(int i = 0; i<S.length(); i++) { myimage = myimage + "<IMG SRC=" + S.charAt(i) + ".gif>"; } return myimage; } %> <% Integer number=(Integer)application.getAttribute("Count"); if(number==null) { number=new Integer(1); application.setAttribute("Count",number); } else { number=new Integer(number.intValue() + 1); application.setAttribute("Count",number); } %> 您是第<%=G((Integer)application.getAttribute("Count"))%> 个访问本站的客户。 </BODY></HTML> 函数G首先取出application("Count")的值,然后赋值给变量S,再执行循环语句,S.length()功能是取字符串的长度,S.charAt(i)的意思是从字符串S的第i个位置开始取1个字符。执行完后就将原先的字符数字转化成以图形显示的图形计数器。本程序执行需要有0~9的十个Gif图片,运行的结果如图5-24所示。 图5-24 基于图形界面的计数器 |
|