Applications, Sessions, and Global.asa

Source: Internet
Author: User
Tags sessions variable
Application|session One of the things that most new ASP users find confusing is the concept of Sessions and applications. Add to this "fact that there ' s" special file on the Web server called Global.asa so is somehow related (and yet C An ' t is requested via a browser and it makes for a very confusing situation. That ' s why I ' m writing this article. This isn ' t going to being a comprehensive discussion of everything you can do using Sessions, applications, and the global.as A, but is instead meant as a introductory level overview to try and alleviate some of the confusion and misconceptions as Sociated with these topics.

Sessions

Of the topics I ' m covering, people usually have the easiest time with the concept of a sessions so I ' ll cover it a. This is the probably stems from the fact, almost all computing used to is done this way. You ' d-Log on, does what you needed to does and then log off. In fact most programs still work that way. For example if you start Windows the the the ' the ' the ' thing you are log in. Then you did what you have to do and the your done with your log off. Similarly, when you are need to did something in a application, you are open the application, do what your want to do and then Clos E it. This scenario are 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 are like using a word proccessor so with every word your type forgets what document it belongs in or even that it ' s Y OU typing it!

So if no is maintained then the Web server doesn ' t know who your are, if you still reading the content it gave , or even if you ' re already long gone. It really doesn ' t care and if you are about it, the concept really does make sense for static pages. If you are just publishing a document for everyone to the, what difference does it make the who you are or what you ' ve already Do 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 On between requests? OK as you ' d expect (or at least infer to I rambling on it) the Web server doesn ' t!

Now along comes ASP and suddenly we are not serving static pages anymore. Some people get one thing and others get something entirely. And not just that ... we want to let a user does 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? So that's would certainly make our lives easier but I don ' t do you like visitors the Web site your like it would very

So, as always seems to happen while 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 a identifier so that's the server knows which set of sessions information on the server is Associat Ed with which requests. So it's important to the "while all" information in a session is actually stored on the server and don't transmitted to The browser, the fact remains that cookie need to is enabled in order for the "server to track a" users session. Using This combination the server side storage and cookies, ASP provides us with a nice neat way to use this information WI Thout has to worry about how it's actually done. This interface was called the session object.

While the session object does have some useful properties and methods, the most important, about it are that you can P Lace variables in it and it'll maintain them for your until the session ends. For example:

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

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

Those of the WHO were paying attention probably caught that I mentioned this variable are only good until the Sessio N ends and yet I didn ' t tell you the happens. This is the main problem with sessions and why they ' ve gotten such a bad reputation. You are 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 is their final one or if they ' re going to is right back. This is leaves us with somewhat of a dilemma. If We wait too long then we ' re storing all the users information and using the "on" that could almost CE Rtainly be better used for something El



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.