Maintaining Persistent Information

Home  »  ASP  »  Maintaining Persistent Information on the Web


     "Working with the Request Object", The Request and Response objects could be used to read and write cookies on clients's computer. Cookies, which can save small bits of simple information on the user's computer, can be used to maintain state over long periods of time. Although cookies offer an easy way to persist information over time, they do have limitations.

To combat these shortcomings, ASP Provides two built-in-objects: the Session object and the Application object. The Session object is designed to maintain state for each visitor to your Web site for the duration of his or her visit. the Application object provides a mechanism to persist non-user-specific information for great lengths of time. By using these two object, you can maintain state across your Web site.

Contents

  • Session Object
  • Application Object
  • Global.asa
  • Ways to Maintain State

       Because the client-server model does not make maintaining state inherently easy, you must examine some advanced techniques to maintain state. To maintain state by sending state information through the querystring.ASP provides some built-in-objects and collections to help maintain state. The Cookies colection, can be used to maintain simple state information over lengthy periods of time. The Session and Application objects. "The Session Object" and "The Application Object", can also be used to maintain state. By using Active Server Pages, state maintain is easier to understand.




    Session Object

    Active Server Pages comes with a built-in object to help developers maintain state on a user-by-user basic. this object is called the Session object and can be accessed through any ASP page on your Web site. The Session object can store any kind of data type, from numbers and strings to arrays and object!

    The Session is used to maintain state only for the duration of a user's visit to your web site. When each new user comes to your site, memory on the Web server is allocated to store the Session obeject for that user.
    Each variable stored in the Session object is referred to as a session variable. You can create session variables with the following syntax:



    What does a Session Start?

    A session starts when:
    1. A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure.
    2. A value is stored in a Session variable.
    3. A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope


    What does a Session End?

    A session ends if user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes.

    If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property.

    The example below sets a timeout interval of 5 mintues:



    To end a session immediately, you may use the Abandon method:



    where SessionVariableName is string. The following lines of code create a number of session variables:



    Create a HTML Form for Welcome.asp


    The Output of Login.html



    Using the Session Object to maintain State



    Output of Welcome.asp



    To maintain state using the session object, you need to create a session variable for each bit of information that needs to be persisted. Because you only need to save the user's name, you can use just one session variable.

    Demo - Session Object

    What is Your Name?






    Application Object

    An application on the Web may be a group of ASP Files. The ASP files work together to perform some purpose. The Application object in ASP is used to tie these files together.

    The Application object is used to store and access variables from any page, just like the Session Object. The difference is that ALL users share one Application object, while with Sessions there is one Session object for EACH user.

    The Application object should hold information that will be used by many pages in the application (like database connection information). This means that you can access the information from any page. It also means that you can change the information in one place and the changes will automatically be reflected on all pages. You can create session variables with the following syntax:




    Lock and Unlock - Application Object

    You can lock and application with the "Lock" method. When and application is locked, the users cannot change the Application variables (other than the one currently accessing it). You can unlock an application with the "Unlock" method. This method removes the lock from the Application variable:






    Global.asa

    To handle the code on the OnStart event of the Session and Application object. To handle Session and Application events, a special file needs to be created. This file must be named, precisely, Global.asa and needs to be placed in the root directory of your web site. When a new user comes to your site and Global.asa exists, the correct event handle in Global.asa is executed.

    A Complete Global.asa



    Another Example of Global.asa with database...






    Share on Google Plus
      Blogger Comment
      Facebook Comment