Sự kiện ASP Application_OnStart và Application_OnEnd
Định nghĩa và cách sử dụng
Sự kiện Application_OnStart
Sự kiện Application_OnStart xảy ra trước khi tạo mới một phiên (khi đối tượng Application được tham chiếu lần đầu tiên).
Sự kiện này được đặt trong tệp Global.asa.
Chú thích:Việc tham chiếu đến đối tượng Session, Request hoặc Response trong vũ trụ Application_OnStart sẽ gây ra lỗi.
Sự kiện Application_OnEnd
Sự kiện Application_OnEnd xảy ra khi ứng dụng kết thúc (khi máy chủ web ngừng chạy).
Sự kiện này được đặt trong tệp Global.asa.
Chú thích:Phương thức MapPath không thể sử dụng trong mã Application_OnEnd.
cú pháp
<script language="vbscript" runat="server"> Sub Application_OnStart ... End Sub Sub Application_OnEnd ... End Sub </script>
thực thể
Global.asa:
<script language="vbscript" runat="server"> Sub Application_OnEnd() Application("totvisitors")=Application("visitors") End Sub Sub Application_OnStart Application("visitors")=0 End Sub Sub Session_OnStart Application.Lock Application("visitors")=Application("visitors")+1 Application.UnLock End Sub Sub Session_OnEnd Application.Lock Application("visitors")=Application("visitors")-1 Application.UnLock End Sub </script>
Hiển thị số lượng người truy cập hiện tại trong tệp ASP:
<html> <head> </head> <body> <p> Có <%response.write(Application("visitors"))%> người truy cập! đang trực tuyến! </p> </body> </html>