ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[±Ð¾Ç] (ASP±Ð¾Ç²Ä9½g) Application »P Session ª«¥ó

[±Ð¾Ç] (ASP±Ð¾Ç²Ä9½g) Application »P Session ª«¥ó

Application°O¿ýª«¥ó
Application ª«¥ó¬O¥Î¨ÓºÞ²z¾ã­ÓÀ³¥Îµ{¦¡¡A¥i´£¨Ñµ¹«È¤áºÝ¨Ï¥ÎªÌ ¦@¨É¸ê°T¡A¦b¹ê°È¤W·|³Q¥Î¨Ó°lÂܥثe¨Ï¥Î¤¤ªº¤H¼Æ¤Î°w¹ï¯S©w¨Ï¥ÎªÌÅã¥Ü¯S©w¸ê®Æ¡C

§Q¥ÎApplication ª«¥ó¨Ó°O¿ýÅܼƤº®e¡A«hºô­¶µ²§ô°õ¦æ®É¨äÅܼƤº®e¤´¥i«O¯d¡A¤]´N¬O»¡Application ª«¥ó¬O¤@­Ó¡yÀRºA¡zÅܼơA¦p¤U©Ò¥Ü:
  1. <%
  2. var=application("var")
  3. response.write "¶}©l var=" & var & "<br>"
  4. var=var+10
  5. response.write "µ²§ô var=" & var & "<br>"
  6. application("var")=var
  7. %>
½Æ»s¥N½X
°õ¦æµ²ªG±N¦p¤U¹Ï¡G
1.png

Application ª«¥ó¬°¡y¦@¥Î¸ê°T¡z¡A¦b¤£¦PÂsÄý¾¹°õ¦æ©Î«ö­«·s¾ã²z®É ¡A¨ä­È¬O²Ö¥[ªº¡A¨Ò¦p§Ú­Ì­«·s°õ¦æApp01.asp ºô­¶¡A§Ú­Ì¥i¥H²M·¡ªºµo²{¡Gvar ÅܼƤºªº­È¤´µM¬°¤W¤@¦¸°õ¦æ«á«O¯d¤U¨Óªº°õ¦æµ²ªG¡A¦Ó·|²£¥Í¤U¹Ïªºµ²ªG¡G
1-2.png

Application ª«¥óªº¥Í©R¶g´Á¡G°_©l©ó PWS/IIS ¶}©l¹B§@¥B¦³¤H¶}©l³s½u®É¡C ²×¤î©ó PWS/IIS Ãö³¬©Î¤@©w®É¶¡¤º(¹w³]¬°20¤ÀÄÁ)¨S¤H³s½u®É¡C ¤]´N¬O»¡ Application ª«¥ó¨Ã¤£¬O¥Ã»·¦s¦bªº¡I°£«D§Ú­Ì±N Application ª«¥ó¤¤ªº¸ê®Æ¼g¿ý¨ìÀɮפ¤±N¨ä«O¦s°_¨Ó¡A¦b«áÄòªº³æ¤¸¤¤¡A§Ú­Ì±N·|¯S§O¤¶²ÐÀɮ׸ê®Æªº¼g¿ý»PŪ¨ú¡C

ÁöµM Application ª«¥ó¥i¥H¦bºô­¶µ²§ô«á¡A±N¨äª«¥ó¤ºªº¸ê®Æ«O¯d¤U¨Ó¡A¦ý¬O¡A·í¦³¨â¦ì¬Æ¦Ü¨â¦ì¥H¤WªºÂsÄýªÌ¦P®É¶i¦æºô­¶Áåµ²®É¡A«h¥¦­Ì©ÒÀò±oªº¸ê®Æ­È±N¬O¬Û¦Pªº¡A¶i¦Ó¨Ï§Ú­Ì²Î­pªº¸ê®Æ²£¥Í¿ù»~¡F¬°¤FÁקK¨â¦ì¬Æ¦Ü¨â¦ì¥H¤WªºÂsÄýªÌ¦P®É¶i¦æºô­¶Áåµ²®É³y¦¨ASPµ{¦¡°õ¦æ¿ù»~¡A¥i¥H§Q¥ÎApplication ª«¥óªº¾Þ§@¤èªk¡G Application.Lock ª«¥ó¤WÂê »PApplication.Unlock ¨ú®øª«¥ó¤WÂê ¡A¦p¤U©Ò¥Ü:
1-3.png

¦p¦¹¥iÁקK¨â¦ìÂsÄýªÌ¦P®ÉÂsÄýºô­¶®É¡Acount­È¥[Á`¤Ö¥[¡C §Yµ{¦¡°õ¦æ¦Ü¡yApplication.lock¡zµ{¦¡±Ô­z«á¡A´N§â Application ª«¥ó¤WÂê¡A¦p¦¹¤@¨Ó Application ª«¥ó«K¤£¯à³Q¨ä¥Lµ{¦¡³s½uªÌ©Ò©I¥s¨Ï¥Î¡A¶·µ¥¨ì©I¥s¡yApplication.lock¡zªºµ{¦¡³s½uªÌ¦A¦¸©I¥s¤F¡yApplication.unlock¡z¨ú®ø Application ª«¥ó¤WÂê«á¡A¨ä¥¦µ{¦¡³s½uªÌ¤~¥i©I¥s Application¡C
  1. <%
  2. application.lock
  3. application("count")=application("count")+1
  4. application.unlock
  5. %>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

Session°O¿ýª«¥ó
Session ª«¥ó¬°¬ö¿ý¨Ï¥ÎªÌªº¬ÛÃö¸ê°T¡A´£¨Ñ¨Ï¥ÎªÌ¦A¦¸¹ï¦¹ºô­¶¦øªA¾¹­n¨D®É§@½T»{¡A¨Ò¦p¨Ï¥ÎªÌ±b¸¹»P±K½Xªº½T»{¡A¦³ Session ª«¥óªº«Ø¥ß¡A¨Ó«O¯d¨­¤À»{ÃÒªºµ²ªG¡A«h¨Ï¥ÎªÌ¤£¥Î©ó¨C¤@­¶ºô­¶µn¿ý®É³£»Ý¿é¤J±K½X§@½T»{¡C

Session ª«¥ó»P Application ª«¥ó¬Û¦P¡A³£¬O¥Î¨Ó°O¿ý¡yÅܼơz­Èªº¡A¦ý¬O Application ¬O¤@¹ï¦hªº¡FSession ª«¥ó«h¬O¤@¹ï¤@ªº¡A¹ï©ó©Ò¦³ªº³s½uÂsÄýªÌ¦Ó¨¥¡A¥L­Ì¹ï©ó Application ª«¥óªº¨Ï¥Î¬O¡y¦@¥Î¡zªº¡A¦ý¬O¨C­ÓÂsÄý³s½uªÌ«o­Ó§O¾Ö¦³¤@­Ó¡y¨p¥Î¡zªº Session ª«¥ó¡A­Y±N¤W¸` app01.asp ¨Ò¤l¤¤¤§¤º®e application §ï¥H session´À¥N¡A¦p¤U©Ò¥Ü:
  1. <%
  2. var=session("var")
  3. response.write "¶}©l var=" & var & "<br>"
  4. var=var+10
  5. response.write "µ²§ô var=" & var & "<br>"
  6. session("var")=var
  7. %>
½Æ»s¥N½X
°õ¦æµ²ªG±N·|¦pApplicationª«¥ó¤@¼Ë¡G
2.png

ÁöµM¦bªí­±¤W¬Ý¨Ó¡Aµ²ªG¬O¬Û¦Pªº¡A¦ý¬O¡ASession ª«¥ó²{¦b¬ö¿ýªºÅܼƸê®Æ¥u»P¥Ø«eÂsÄý³s½uªÌ¦³Ãö¡A»P¨ä¥LªºÂsÄý³s½uªÌ¤@ÂIÃö«Y³£¨S¦³¡A¤]´N¬O»¡¥Ø«e©ÒŪ¨úªº Session ª«¥ó¤º®e¬OŪ¨ú¥Ø«eÂsÄý³s½uªÌ¡y¨p¥Î¡zªº Session ª«¥ó¡C

Session ª«¥ó»P Applicationª«¥ó¤§¤£¦PÂI ¡GApplicationª«¥ó¬°¦@¥Î¤§¸ê°T¡A¦ÓSession«o¬O­Ó§O¿W¥ß ªº¡A¤ñ¸û¤U¨â¹Ï¡A¤£¦PÂsÄý¾¹°õ¦æsess01.asp¡A¨ä­È¬O¤£²Ö¥[ªº¡A¥Ñ¦¹§ó¥iÃÒ©ú¡GSession ª«¥ó¬ö¿ýªºÅܼƸê®Æ¥u»P¥Ø«eÂsÄý³s½uªÌ¦³Ãö¡A»P¨ä¥LªºÂsÄý³s½uªÌ¤@ÂIÃö«Y³£¨S¦³¡C
3.png

Session ª«¥óªº¥Í©R¶g´Á °_©l©ó PWS/IIS ÂsÄý¾¹²Ä¤@¦¸»P¦øªA¾¹³s½u®É¡C ²×¤î©ó PWS/IIS ÂsÄý¾¹µ²§ô°õ¦æ®É©ÎÂsÄý¾¹¤@¬q®É¶¡¨S¦³¦VPWS/ IIS­n¨D¥ô¦óºô­¶®É¡C

¹ê°µ¾Ç²ß¡G
·í§Ú­Ì§Q¥Î Application ¨Ó§@¬°¶i¯¸¤H¼Æªº­p¼Æ¾¹¡A¬O§Q¥Î·í¦³·sªºÂsÄý³s½uªÌ¶i¤J®É´N©I¥s Application ª«¥ó¡A±N Application ª«¥ó©ÒÀx¦sªºÅܼƸê®Æ­È¥[¤@¨Ó¹F¨ì­p¼Æªº¥Øªº¡A¦ý¬O¡A¤WºôªÌ¥u­n«ö¤UÂsÄý¾¹¤Wªº¡y­«·s¾ã²z¡z«ö¶s«á¡A§A·|µo²{­p¼Æ¾¹±N·|¦A¦¸ªº¦Û°Ê¥[¤@¡A³o¦ü¥G¤£¬O§Ú­Ì·Q­nªº¡C³æ¯Âªº Application ­p¼Æ¾¹µ{¦¡½X»P°õ¦æµ²ªG¡A¦p¤U©Ò¦C¡G
4.png
  1. <%
  2. Application.Lock
  3. Application("counter") = Application("counter") + 1
  4. Application.UnLock
  5. %>

  6. <HTML>
  7. <BODY>
  8. <CENTER><H2>Application­p¼Æ¾¹<HR></H2>
  9. ¶i¯¸¤H¼Æ <%=Application("counter")%> ¦ì
  10. </BODY>
  11. </HTML>
½Æ»s¥N½X
¨º§Ú­Ì¸Ó¦p¦ó¨¾¨î³s½uªÌ«ö¤U¡y­«·s¾ã²z¡z«ö¶s¨Ï­p¼Æ¾¹¦Û°Ê¥[¤@©Ò²£¥Íªº¿ù»~©O¡H§Ú­Ì¥i¥H§Q¥Î¨ã¦³¡y¨p¥Î¡z¯S©Êªº Session ª«¥ó¨Ó§PÂ_ÂsÄý³s½uªÌ¬O§_¬O·sªº³s½uªÌ¡A§Ú­Ì¥i¥H§Q¥Î¤U¦C§PÂ_¦¡¥ýÀËÅç¤@¤U¡A¦pªG¬O·sªº³s½uªÌ¡A«h¨äSessionª«¥ó¬O¨S¦³¥ô¦ó¸ê®Æªº¡I
  1. If IsEmpty (Session("Conn")) Then
½Æ»s¥N½X
¦pªG¤W¦Cªº§PÂ_¦¡¬O¦¨¥ßªº(ÂsÄý³s½uªÌ¬°·s³s½uªº¡ASessionª«¥ó¤º¨S¦³¥ô¦óªº¸ê®Æ)¡A«h§Ú­Ì´N±NApplication ª«¥óªº¬ö¿ý¸ê®Æ¥[¤@¡A¦P®É±NÂsÄý³s½uªÌªºSession("Conn")¸ê®Æ­È³]¬°¡yTrue¡z¡A¦p¦¹¤@¨Ó¡A·íÂsÄý³s½uªÌ¦A¦¸«ö¤U¡y­«·s¾ã²z¡z«ö¶s«á¡A¦]¬°If IsEmpty (Session("Conn")) Then§PÂ_¦¡¤£¦¨¥ß(Sessionª«¥ó¤º¤w¦³³s½u¬ö¿ýªº¸ê®Æ)¡A«hApplication ª«¥óªº¬ö¿ý¸ê®Æ´N¤£·|¦A¦Û°Ê¥[¤@¡C¨Ï¥Î Application ª«¥ó»P Session ª«¥ó¼¶¼gªº­p¼Æ¾¹µ{¦¡½X»P°õ¦æµ²ªG¡A¦p¤U©Ò¦C¡G
5.png
  1. <%
  2. If IsEmpty(Session("Conn")) Then
  3. Application.Lock
  4. Application("counter") = Application("counter") + 1
  5. Application.UnLock
  6. End If
  7. Session("Conn") = True
  8. %>

  9. <HTML>
  10. <BODY>
  11. <CENTER><H2>Session­p¼Æ¾¹<HR></H2>
  12. ¶i¯¸¤H¼Æ <%=Application("counter")%> ¦ì
  13. </BODY>
  14. </HTML>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ¦³¦h¤Ö¤O¶q´N°µ¦h¤Ö¨Æ¡A¤£­n¤ß¦sµ¥«Ý¡Aµ¥«Ý¤~·|¸¨ªÅ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD