欢迎光临
我们一直在努力

Applications, Sessions, and Global.asa

建站超值云服务器,限时71元/月

one of the things that most new asp users find confusing is the concept of sessions and applications. add to this the fact that theres this special file on the web server called global.asa that is somehow related (and yet cant be requested via a browser) and it makes for a very confusing situation indeed. thats why im writing this article. this isnt 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 im covering, people usually have the easiest time with the concept of a session so ill cover it first. this probably stems from the fact that almost all computing used to be done this way. youd 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) doesnt maintain state. this is like using a word proccessor that with every word you type forgets what document it belongs in or even that its you typing it!

so if no state is maintained then the web server doesnt know who you are, if youre still reading the content it gave you, or even if youre already long gone. it really doesnt care and if you think about it, the concept really does make sense for static pages. if youre just publishing a document for everyone to see, what difference does it make who you are or what youve already done if youre 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 youd expect (or at least infer from my rambling on about it) the web server doesnt!

now along comes asp and suddenly were 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 dont 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 its 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 its 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 couldnt guess is my first name). now that ive 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 doesnt 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 didnt tell you when that happens. this is the main problem with sessions and why theyve gotten such a bad reputation. you see, since we dont know if a users going to come back or not (because of the stateless nature of http) we dont 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 theyre going to be right back. this leaves us with somewhat of a dilemma. if we wait too long then were storing all the users information and using up resources on the server that could almost certainly be better used for something else. if we dont wait long enough then the user might come back and the server might have already deleted all their information and theyd have to start over. finding the right balance can be difficult and varies from site to site. as a starting point, the default session timeout is 20 minutes. what this means is that after each request the server will wait 20 minutes or until a user comes back before deleting the users information.

because this is such a hangup for people, ill say it again… a session ends 20 minutes after the users last request! not when they close their browser! not when they go somewhere else! 20 minutes later!

because of this inefficiency of storing of state after a user has left, many people have resorted to not using sessions at all. while (imho) this is a little bit of overkill, it does raise some valid points. first of all, if you decide to use sessions in your site, try and store relatively small amounts of data in them. this also means you should never store actual db objects or things like that in them! while most people do this expecting to gain speed and save resources by not having to close and reopen the objects, you actually lose performance and eat up more resources! let mts (microsoft transaction server) work the way it was meant to and do the pooling for you. just create and kill the objects on each page as you need them, preferably creating them as late as possible and deleting them as early as possible. (and yes this means you need to close and set to nothing all those ado objects!)

applications

ok so now you know what an asp session is so whats an asp application? well in general terms an application is basically a program or group of programs designed for end users to use. these include database programs, word processors, spreadsheets, etc. well on the web we have groups of asp files and to group them together into functional units or a complete program theres the concept of an application. in the basic sense this is the equivilent of a normal program in that its a group of files that work together to perform some purpose. to tie these files together, asp has an application object.

the application object works similarly to a session object in the sense that you can store variables in it and access it from any page, but it differs in the fact that all users share one application object. so, if one user runs an asp file that changes the value of something of application scope, any future access of it by that user or any other will return the new value. this also means that since the application object isnt related to any specific user session, it doesnt have any of the associated problems that session objects do. this makes it a little easier to use and, while you always need to be careful, youre much less likely to bring a server to its knees using the application object then you are using the session object.

the primary purpose of using the application object is to centralize information that is to be used in many places throughout the application. for example, many users store db connection information there. this allows you to reference the information on any page on which it might be needed and also lets you change it in one place and have that change automatically reflected wherever you use it. its still not a great idea to be storing lots of objects in it, but used carefully the application object can be a great asset to almost any web site.

global.asa

at this point i can almost hear you saying "okay, so now we know all about sessions and applications, but why is this one file covered in the same article and what the heck does it have to do with all this stuff about user state and global variables?" well ill tell you. its often useful to be able to do stuff at the beginning and end of something. for example when you get up in the morning you perform some basic steps to start your day. you take a shower, brush your teeth, get dressed, etc. you do these things so youre able to function better throughout the day. well computers work the same way. before you can expect a session or application to be useful and work its best, you essentially need to bring it its morning cup of coffee. this is where the global.asa file comes in.

in the global.asa you can tell the application and session objects what they should do when they start and end. these instructions are placed into what are called event handlers. there are four such event handlers supported in global.asa. theyre named based on their functions so logically enough they are: application_onstart, application_onend, session_onstart, and session_onend. these subroutines can contain most any asp code you want them to with a few exceptions. for example you cant use response.write because since global.asa is never called by a browser it naturally doesnt return anything to the browser.

id like to take this opportunity to drive home the point that session_onend fires when the session ends and not when the user leaves your site or closes their browser. so by default this happens 20 minutes later and the user is long gone! this is often a source of much confusion for beginners and i thought i should just set the record straight while i had the chance.

in order for the web server to know to look for this file you need to set up your directory as an application in iis. this is done by navigating to the appropriate web site or directory in internet services manager and right clicking on it and selecting "properties" from the pop up menu. then select the "directory" or "home directory" tab and on the bottom half of that window you will see the "application settings" section. if its not already, you need to make the directory an application by pressing the "create" button. click "ok" to save the setting and exit.

in pws (personal web server) on windows 95/98 this is done differently. you need to go to the personal web manager and then click on "advanced" in the bottom left. in the directory tree you should see an entry named <home>. select it and click the "edit properties…" button. in the resulting dialog box ensure that all three checkboxes are checked (read, execute, and scripts). click "ok" to save the setting and exit. this will enable processing of the global.asa in pws.

so now that youve got an idea what they are and how to use them, you can use applications, sessions, or both in your asp projects… and you should! while they can be a little confusing and cause problems if youre not careful, they can also be extremely powerful tools to help you write faster and more efficient asp.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Applications, Sessions, and Global.asa
分享到: 更多 (0)