您的位置:首页精文荟萃软件资讯 → Developing ASP-Based Applications

Developing ASP-Based Applications

时间:2004/10/7 19:04:00来源:本站整理作者:蓝点我要评论(0)


            
             
              
             
            

               
               

            



            An ASP-based application consists of a virtual directory on a Web server and all the folders and files within that virtual directory. For more information about virtual directories, refer to your Microsoft Web server online documentation.

An application can be a simple home page; it can include a number of dynamic elements, such as the custom home page of the MSN™ online service (www.msn.com); or it can consist of a complex set of interrelated pages and logic.

When you use ASP-based applications, you are able to maintain state. State is the ability to retain information. You can use ASP to maintain two types of state:

Application state, in which all information pertaining to an application is available to all users of an application.
Session state, in which information is available only to a user of a specific session.
The ASP tools you use to manage state are the Session and Application built-in objects.

Using the Session and Application Objects
You can use the ASP built-in objects Session and Application to extend the functionality of your ASP-based applications.

Use the Session object to manage information for a user when that user is using an application. A session belongs, in effect, to a single user. The Application object is used to store common information that can be shared between all users of a single ASP-based application.

Using the Global.asa File
Each ASP-based application can have one Global.asa file. (The file name extension .asa stands for “Active Server Application.”) This file must be stored in the root directory of the application. ASP reads a Global.asa file when:

The Web server receives the first post-startup request for any .asp file in a given application; that is, after the Web server starts, the first request for any .asp file in an application causes ASP to read the Global.asa file for that application.
A user who does not have a session requests an .asp file in an application.

You can include the following in a Global.asa file:

Application-start events, session-start events, or both.
Application-end events, session-end events, or both.
Object tags. You can use the tag to create objects in a Global.asa file. Refer to Setting Component Scope for information about creating objects with this tag.

Refer to Global.asa Reference for more information.

Application-Start and Session-Start Events
The application-start and session-start events are Application_OnStart and Session_OnStart, respectively. You should include in these procedures scripts that you want to run whenever an application or session starts. If an application and a session start at the same time, ASP processes the application-start event before it processes the session-start event.

Use the following syntax to define an application-start event:



To create an instance of the Ad Rotator component whenever a session starts, you could define the following procedure:



Application-End and Session-End Events
The application-end and session-end events are Application_OnEnd and Session_OnEnd, respectively. Like the application-start and session-start events, these events are procedures that you include in a Global.asa file. Unlike start events, end events occur only when a session or application ends; thus, you should include in them any scripts that you want to run at those times. If a session and an application end at the same time, ASP processes the session-end event before it processes the application-end event.

Use the following syntax to define a session-end event:



Use the following syntax to define an application-end event:



Ending a Session
A session automatically ends if a user has not requested or refreshed a page in an application for a specified period of time. This value is 20 minutes by default. (Refer to Configuring Registry Entries for information about changing the default value).

You can explicitly end a session with the Abandon method of the Session object. For example, you can provide a Quit button on a form with the ACTION parameter set to the URL of an .asp file that contains the following command.

<% Session.Abandon %>

If, for a specific session, you want to set a timeout interval that is longer than the 20-minute default, you can set the Timeout property of the Session object. For example, the following script sets a timeout interval of 30 minutes.

<% Session.Timeout = 30 %>
Note You cannot set the timeout interval to be less than the default value.

Ending an Application
An application ends when the Web server is shut down.

Managing Sessions
You can use the Session object to set objects or variables to have session scope. Scope is the extent to which a component instance, object, or variable is available within Active Server Pages. A variable that has session scope, then, is accessible only within that session.

A session can begin in three ways:

A new user requests a URL that identifies an .asp file in an application, and the Global.asa file for that application includes a Session_OnStart procedure.
A user stores a value in the Session object.
A user requests an .asp file in an application, and the application’s Global.asa file uses the tag to instantiate an object with session scope. See Setting Component Scope for more information about using the tag to instantiate an object.

SessionID and Cookies
The first time a user requests an .asp file within a given application, ASP generates a SessionID, then sends a response to the user’s browser to create a cookie for the SessionID. The SessionID is a number produced by a complex algorithm that identifies the user’s session. The SessionID cookie is a token sent to a client browser that is not stored on the client computer's hard disk because it does not set an expiration date.

Note While most browsers support cookies, some do not. If a user’s browser does not support cookies, ASP does not support the Session object for that browser.

The SessionID cookie is similar to a locker key in that, as the user interacts with an application during a session, ASP can store information for the user in a “locker” on the server. The user’s SessionID cookie, which it sends in the HTTP request header, enables access to this information in the way that a locker key enables access to a locker’s contents. Each time that ASP receives a request for a page, it checks the HTTP request header for a SessionID cookie.

Storing Variables in the Session Object
You need only reference a new variable in order to create and store the variable in the Session object. For example, the following commands store three new variables in the Session object:

<%
Session("Initiated") = Now
Session("Fidelity") = "Low"
Set Session("myObj") = Server.CreateObject("someObj")
%>


Remembering User Preferences
You can store user preferences in the Session object. For example, you can allow a user to specify a text-only version of your content in the first page of the application and apply this choice on all subsequent pages that the user visits in this application.

<%If Session("Fidelity") = "Low" Then %>
This is the text version of the page.
<% Else %>
This is the multimedia version of the page.
<% End If %>

Managing Applications
You can use the Application object to set properties that are accessible to every user in an ASP-based application.

Posting Messages to Application Users
You can use the Application object to post a message that each user of the application sees on entering the application. This section shows an example in which you define an application property called message in an Application_OnStart procedure, then enable subsequent users to modify the message.

The application-wide message is given a default value in Global.asa:



A.asp displays the message. Every user who requests A.asp can see the global message.



This is the message:


<%= Application("Message") %>



B.asp provides a way for a user to type a new message and submit the change.




Enter a new value for the application-wide message:







C.asp resets the global message to the value received from a user in B.asp and redirects the user to A.asp to see the new value.

<%
If Not IsEmpty(Request.Form("newmsg")) Then
Application.Lock
Application("Message") = Request.Form("newmsg")
Application.Unlock
End If
Response.Redirect("A.asp")
%>

Setting Component Scope
An ASP-based application can set ActiveX server components to have application, session or page scope.

An application-scope component instance is a single instance of a component that is created when the application starts. This instance is shared by all client requests.
A session-scope component instance is created for each new session in an application and released when the session ends; thus, there is one instance per active session.
A page-scope component instance is created for the processing of a page for one client; it is available through the processing of that page; and is then released when the response is sent back to the client. A component instance has page scope by default.

You can use either the tag in a Global.asa file or the Server.CreateObject method to store instances of ActiveX server components in Session and Application objects.

Using the Tag
You can declare component instances with session or application scope in a Global.asa file by using the tag, extended with RUNAT attribute (which must be set to Server) and SCOPE attribute (which can be set to Session or Application). You can accomplish this by using either the registered name (PROGID) method or the registered number (CLASSID) method.

The following example uses the registered name (PROGID) method to create a session-scope instance of the Ad Rotator Component:





The following uses the registered number (CLASSID) method to create an application-scope instance of the Ad Rotator Component:

CLASSID="Clsid:00000293-0000-0010-8000-00AA006D2EA4">


When you declare a session-scope or application-scope instance of a component by using the tag, the variable you assign to the component goes into the session or application namespace, respectively. This means that you do not need to use the Session or Application built-in objects to access the component instance. For example, the following script command, issued from within any .asp file that is part of the application containing the examples shown above, would open the instance of the Ad Rotator component declared in those examples:

<%= MyAd.GetAdvertisement("addata.txt") %>


You can also use the tag to create ActiveX server component instances in a particular .asp file. In this case the SCOPE attribute can be omitted, or set to PAGE. All such component instances have page scope.

Using the Server.CreateObject Method
You can use the Server.CreateObject method to store an instance of a component in the Session object. The following example stores an instance of the Ad Rotator component in the Session object.

<% Set Session("MyAd") = Server.CreateObject("MSWC.Adrotator") %>


To display an ad, you would include the following:

<% Set MyAd = Session("MyAd") %>
<%= MyAd.GetAdvertisement("addata.txt") %>


Performance Issues
ASP does not instantiate a component that you declare with the tag until that component is referenced by a script command from an .asp file. The Server.CreateObject method instantiates the component immediately. Thus, the tag offers better performance than the Server.CreateObject method for session-scope components.

For creating page-scope components, the tag and the Server.CreateObject method offer equivalent performance.

You can use the tag to store single-threaded, free-threaded, or apartment-threaded objects in both the Session and the Application objects. You can use the Server.CreateObject method to store single-threaded, free-threaded, and apartment-threaded objects in the Session object. However, whether you use the tag or the Server.CreateObject method, unless the object you store is marked “both,” ASP treats the application or session containing this object as “single-threaded.” Thus, storing objects that are not marked “both” may have performance implications, particularly for high-volume sites. Refer to Creating Components for ASP for more information on threading models.

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3

最新文章 微信3.6.0测试版更新了微信支付漏洞会造成哪 360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本