Applications, Sessions, and Global.asa

application|session

One of the things that most new ASP users find confusing is the concept of Sessions and Applications. Add to this the fact that there's this special file on the web server called global.asa that is somehow related (and yet can't be requested via a browser) and it makes for a very confusing situation indeed. That's why I'm writing this article. This isn't going to be a comprehensive discussion of everything you can do using Sessions, Applications, and the Global.asa, but is instead meant as an introductory level overview to try and alleviate some of the confusion and misconceptions associated with these topics.

Sessions

Of the topics I'm covering, people usually have the easiest time with the concept of a session so I'll cover it first. This probably stems from the fact that almost all computing used to be done this way. You'd log on, do what you needed to do and then log off. In fact most programs still work that way. For example when you start Windows the first thing you is log in. Then you do what you have to do and when your done you log off. Similarly, when you need to do something in an application, you open the application, do what you want to do and then close it. This scenario is similar to a session, but when it comes to the web there is one problem... the protocol of the web (HTTP) doesn't maintain state. This is like using a word proccessor that with every word you type forgets what document it belongs in or even that it's you typing it!

So if no state is maintained then the web server doesn't know who you are, if you're still reading the content it gave you, or even if you're already long gone. It really doesn't care and if you think about it, the concept really does make sense for static pages. If you're just publishing a document for everyone to see, what difference does it make who you are or what you've already done if you're going to get the same page as everyone else anyway? So the question then becomes, why should the server waste the resources needed to track users and maintain their information between requests? Well as you'd expect (or at least infer from my rambling on about it) the web server doesn't!

Now along comes ASP and suddenly we're not serving static pages anymore. Some people get one thing and others get something entirely different. And not just that... we want to let a user do things that might take more that one request. So what are we supposed to do... make the user tell us who they are and everything about themselves with every request? Well that would certainly make our lives easier, but I don't think visitors to your web site would like it very much!

So, as always seems to happen when we have a problem like this, ASP comes to our rescue and maintains state for us. It does this by creating a unique cookie and sending it to the client. This cookie then acts as an identifier so that the server knows which set of session information on the server is associated with which requests. So it's important to note that while all information in a session is actually stored on the server and not transmitted to the browser, the fact remains that cookies need to be enabled in order for the server to track a users session. Using this combination of server side storage and cookies, ASP provides us with a nice neat way to use this information without having to worry about how it's actually done. This interface is called the Session object.

While the Session object does have some useful properties and methods, the most important thing about it is that you can place variables in it and it will maintain them for you until the session ends. For example:

Session("FirstName") = "John"
will set the Session variable FirstName to John (which if you couldn't guess is my first name). Now that I've stored the value in a session variable I can access it on any subsequent page by simply using:

Session("FirstName")
Except for the fact that it doesn't go away when the page is done executing, it behaves the same as any other variable in ASP and can be used in the same manner.

Those of you who were paying attention probably caught that I mentioned that these variable are only good until the session ends and yet I didn't tell you when that happens. This is the main problem with sessions and why they've gotten such a bad reputation. You see, since we don't know if a users going to come back or not (because of the stateless nature of HTTP) we don't know how long to keep their session alive. Until the next request, we never know if the last one was their final one or if they're going to be right back. This leaves us with somewhat of a dilemma. If we wait too long then we're storing all the users information and using up resources on the server that could almost certainly be better used for something el

时间: 2025-01-27 01:03:49

Applications, Sessions, and Global.asa的相关文章

正确使用ASP中的global.asa

正确使用global.asa是ASP开发者的一个共同问题.最主要的是要知道什么时候该用Virtual Application,什么时候该用Virtual directory,并且知道他们的不同. Virtual Directories(虚拟目录) 简而言之,virtual directories是虚拟出来的,不一定跟web root在同一个的目录上,可以是其他的.但是你可以像使用web root上的其他目录一样使用它.当运行global.asa以后,ASP就把所有的虚拟目录看成是应用程序根目录上

Global.asa 参考(五) - TypeLibrary 声明

参考 ActiveX 组件常常要描述类型库中该组件支持的常量.类型库是一个文件,其中包含有关 ActiveX 组件所支持的对象和类型的信息.如果用户的 Web 应用程序依赖于已在类型库中声明了类型的 ActiveX 对象,就可以在 Global.asa 文件中声明其类型.这样做以后,就可以在应用程序范围内从任何脚本引用已在类型库中声明了的数据类型. 有关在 ASP 中使用常量的详细信息,请参阅"使用变量和常量". 语法<!--METADATA TYPE="TypeLib

什么是Global.asa文件

大家好! Global.asa文件是一个可选文件,在这个文件中,你可以定义事件脚本和使用Session和Application对象.Global.asa文件的内容不能向用户显示,但是它存储的信息能应用于整个应用程序.这个文件必须命名为Global.asa,并且存储在应用程序的启动点的目录下面,一个应用程序只能有一个Global.asa文件. Global.asa文件只能包含以下内容: 1,Application事件. 2,Session事件. 3,<OBJECT>的声明. 4,类库的声明.

ASP入门:Global.asa文件技巧用法

首先.asa是文件后缀名,它是Active Server Application的首字母缩写.Global.asa文件可以管理在ASP应用中两个非常苛刻的对象:Application.Session. 它其实是一个可选文件,程序编写者可以在该文件中指定事件脚本,并声明具有会话和应用程序作用域的对象.该文件的内容不是用来给用户显示的,而是用来存储事件信息和由应用程序全局使用的对象.该文件必须存放在应用程序的根目录内.每个应用程序只能有一个Global.asa文件. 关于Global.asa文件最常

ASP教程:第十篇 Global.asa文件的使用及Chat程序

 在上一篇中作者给大家详细介绍了两个非常实用的 ASP 内建对象 Application 和 Session 的使用方法.由于这两者的 OnStart.OnEnd 事件的脚本都必须在 Global.asa 文件中声明 , 因此,本篇将给大家详细介绍 Global.asa 文件的使用方法.为使大家熟练掌握至今所学过的知识,本篇还将举出一个 ASP 的 Chat 程序,供各位参考. 最近很多朋友来信问我,为什么前两期的范例程序运行时有这样那样的错.首先,我要向大家声明,这些程序都是我自己写的,在出"

Global.asa使用手册

Global.asa 文件是一个可选文件,用户可以在该文件中指定事件脚本,并声明具有会话和应用程序作用域的对象.该文件的内容 给用户显示的,而是用来存储事件信息和由应用程序全局使用的对象.该文件的名称必须是 Global.asa 且必须存放在应用程序的 根目录中.每个应用程序只能有一个 Global.asa 文件. Global.asa 文件只能包含如下内容: 1.应用程序事件 2.会话事件 3.<OBJECT> 声明 TypeLibrary 声明 如果包含的脚本没有用 <SCRIPT&

ASP编程入门进阶(十):Global.asa文件

编程 我们已经知道Application和Session对象的OnStart.OnEnd事件的脚本,都必须是在 Global.asa 文件中声明的.那究竟Global.asa是什么样的一个文件?它的作用有何?又该如何运用呢?且听我慢慢道来. 首先.asa是文件后缀名,它是Active Server Application的首字母缩写.Global.asa文件可以管理在ASP应用中两个非常苛刻的对象:Application.Session. 它其实是一个可选文件,程序编写者可以在该文件中指定事件脚

ASP入门进阶之Global.asa文件用法

我们已经知道Application和Session对象的OnStart.OnEnd事件的脚本,都必须是在 Global.asa 文件中声明的.那究竟Global.asa是什么样的一个文件?它的作用有何?又该如何运用呢?且听我慢慢道来. 首先.asa是文件后缀名,它是Active Server Application的首字母缩写.Global.asa文件可以管理在ASP应用中两个非常苛刻的对象:Application.Session. 它其实是一个可选文件,程序编写者可以在该文件中指定事件脚本,并

Global.asa文件用法大全

Global.asa 文件是一个可选文件,用户可以在该文件中指定事件脚本,并声明具有会话和应用程序作用域的对象.该文件的内容给用户显示的,而是用来存储事件信息和由应用程序全局使用的对象.该文件的名称必须是 Global.asa 且必须存放在应用程序的根目录中.每个应用程序只能有一个 Global.asa 文件.Global.asa 文件只能包含如下内容:1.应用程序事件2.会话事件3.<OBJECT> 声明TypeLibrary 声明如果包含的脚本没有用 <SCRIPT> 标记封装