小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

APPLICATION對(duì)象

 鳳舞天煌 2007-12-03
application對(duì)象

站點(diǎn)所有的用戶公用一個(gè)application對(duì)象,當(dāng)站點(diǎn)服務(wù)器開啟的時(shí)候,application就被創(chuàng)建,直到網(wǎng)站關(guān)閉。利用application這一特性,可以方便地創(chuàng)建聊天室和網(wǎng)站計(jì)數(shù)器等常用站點(diǎn)應(yīng)用程序。

1  application的自定義屬性

可以為application對(duì)象添加屬性,application對(duì)象常用的方法有兩個(gè):

(1)public void setAttribute(String key, Object obj),將對(duì)象obj添加到application對(duì)象中,并為添加的對(duì)象添加一個(gè)索引關(guān)鍵字key。

(2)public Object getAttribute(String key),獲取application對(duì)象中含有關(guān)鍵字key的對(duì)象。由于任何對(duì)象都可以添加到application中,因此用此方法取回對(duì)象的時(shí)候,需要強(qiáng)制轉(zhuǎn)化為原來的類型。

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);

%>

程序首先對(duì)application的一個(gè)屬性進(jìn)行賦值,然后又將它取出來輸出到瀏覽器上,程序顯示的結(jié)果如圖5-20所示。

圖5-20  自定義屬性

執(zhí)行完后,該對(duì)象就被保存在服務(wù)器上。執(zhí)行程序5-20.jsp時(shí)依然可以輸出原先保存的值。

案例名稱:自定義屬性

程序名稱:5-20.jsp

<%@ page contentType="text/html;charset=GBK" %>

<%

   String strBack = (String)application.getAttribute("greeting");

   out.print(strBack);

%>

雖然在該程序沒有賦值,但是依然可以輸出,因?yàn)?-19.jsp文件已經(jīng)給application賦值,如圖5-21所示。

圖5-21  讀取application屬性的值

application變量不會(huì)因?yàn)槟骋粋€(gè)甚至全部用戶離開而消失,一旦建立application變量,那么它就一直存在到網(wǎng)站關(guān)閉或者這個(gè)application對(duì)象被卸載,經(jīng)??赡苁菐字芑蛘邘讉€(gè)月。

2  實(shí)現(xiàn)聊天室

聊天室允許多用戶實(shí)時(shí)進(jìn)行信息交流,所有用戶可以看到彼此的信息,這與application對(duì)象的特點(diǎn)正好符合,所以可以方便地利用application實(shí)現(xiàn)聊天室,如程序5-21.jsp所示。

案例名稱:簡(jiǎn)易聊天室

程序名稱: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>

這時(shí)就可邀請(qǐng)一個(gè)朋友進(jìn)入聊天室,雖然比較簡(jiǎn)易,不過已經(jīng)實(shí)現(xiàn)了聊天室的功能,執(zhí)行的結(jié)果如圖5-22所示。

圖5-22  簡(jiǎn)易聊天室

案例5-3:網(wǎng)頁計(jì)數(shù)器

網(wǎng)頁計(jì)數(shù)器是application 對(duì)象的又一個(gè)用途,因?yàn)閍pplication是所有的用戶共有的,所以可以存儲(chǔ)計(jì)數(shù)器的值,當(dāng)有新用戶訪問網(wǎng)頁時(shí)自動(dòng)增加計(jì)數(shù)器的值,如程序CountV1.jsp所示。

案例名稱:網(wǎng)頁計(jì)數(shù)器版本一

程序名稱: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")%>

個(gè)訪問本站的客戶。

</BODY>

</HTML>

程序顯示結(jié)果如圖5-23所示。

圖5-23  網(wǎng)頁計(jì)數(shù)器

一般網(wǎng)站的計(jì)數(shù)器都是圖形界面,這個(gè)計(jì)數(shù)器也可以變成具有圖形界面的計(jì)數(shù)器,如程序CountV2.jsp所示。

案例名稱:網(wǎng)頁計(jì)數(shù)器版本二

程序名稱: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"))%>

個(gè)訪問本站的客戶。

</BODY></HTML>

函數(shù)G首先取出application("Count")的值,然后賦值給變量S,再執(zhí)行循環(huán)語句,S.length()功能是取字符串的長(zhǎng)度,S.charAt(i)的意思是從字符串S的第i個(gè)位置開始取1個(gè)字符。執(zhí)行完后就將原先的字符數(shù)字轉(zhuǎn)化成以圖形顯示的圖形計(jì)數(shù)器。本程序執(zhí)行需要有0~9的十個(gè)Gif圖片,運(yùn)行的結(jié)果如圖5-24所示。

圖5-24  基于圖形界面的計(jì)數(shù)器

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多