Visual Basic Concepts

The Object Model for IIS Applications

IIS applications are hosted by an .asp (Active Server Pages) file and make use of several of the objects in the Active Server Pages object model. The webclass uses these objects to access and manipulate information from an HTML page. The ASP objects that a webclass can use include:

  • Request — Receives requests from end users in the browser.

  • Response — Sends information to the browser in order to display it to the user.

  • Session — Maintains information about the current user session and stores and retrieves state information.

  • Application — Manages state that is shared across multiple webclass instances.

  • Server — Creates other objects and determines server-specific properties that might influence the webclass's processing.

  • BrowserType — Determines the capabilities of the user's browser and makes processing decisions based on that information.

The ASP Request Object

You use the Request object to retrieve information from or about the current user. The Request object gives you access to all of the information passed in any request. HTTP requests contain information about the current user, any data they entered prior to making the request, and arguments that tell the Web server how to process and respond to the request.

Most frequently, you use the Request object to retrieve information from an HTML form. For example, you might retrieve all of the form elements passed back in a Submit event. The following example shows how you might use the Request object to gather information from the browser:

Private Sub Webitem1_Submit
   'Define variables to hold information retrieved from the request
   Dim first as String
   Dim last as String
   'Retrieve form information and assign it to the variables, using
   'the Request object and its Form collection.
   First = Request.Form("Firstname")
   Last = Request.Form("Lastname")
End Sub

You can use the Request object's associated collections to access information. These collections include:

  • QueryString — Retrieves the values of additional arguments in a URL when a request is passed using the . GET is used by the Web server to retrieve objects and, in some cases, to send information from an HTML form.

  • Form — Retrieves the value of form elements passed in an HTTP request when the request is passed using the . POST is a method used by the Web browser to send the information from an HTML form.

    Note   POST is the more common method used to send form information. You can use the Request object to access any of this data. You should not use the GET method in your HTML template files.

  • Cookies — Retrieves the data contained in sent with the form request. Cookies are small parcels of information used to store data about the current user. These can be passed between the browser and the Web server.

  • ServerVariables — Retrieves information such as header values, logon name, or server protocols in use.

  • ClientCertificate — Retrieves information stored in certificate fields when the browser sending the request supports client certificates. Certificates identify a user to the Web server.

For More Information   See "Using Scripting Languages" in the Tools and Technologies section of MSDN for more information on the Request object and its collections.

The ASP Response Object

You use the Response object to return information to the browser. For example, you might use the object's Respond event to write HTML to the browser. There are several methods you can use with the Response object:

  • Use the Write or BinaryWrite method to send information directly to a browser.

  • Use the Redirect method to direct the user to a different than the one the user requested, such as a different webclass or an external page.

  • Use the Cookies collection to set cookie values to return to the browser and store these values for future use. A cookie is a set of information about the user that can be passed between the client and the server, identifying the user to either system.

  • Use the Buffer property to postpone a response while the entire page is processed.

  • Use the AddHeader method to add http headers to a response.

The following example shows how you might use the Response object to write a few lines of HTML code to the browser:

With Response
   'Indicate the beginning of the HTML page
   .Write "<HTML>"
   'Indicate the beginning of the body section
   .Write "<BODY>"
   'Send a line with heading two style followed by
   'a paragraph break
   .Write "<H2>Feedback Form</H2><P>"
   'Send a paragraph of text
   .Write "Please enter your comments below:<P>"
   'other write statements to present form elements
   'and close the HTML document.
End With

For More Information   See "Using Scripting Languages" in the Tools and Technologies section of MSDN for more information on the Request object and its collections.

The ASP Session and Application Objects

The Session and Application objects both store state information about the webclass's end users. The Session object can store information about a single user in the current session. The Application object can store information about multiple users.

You use the Session object to maintain information about the current user session and to store and retrieve state information. For example, suppose you have an application that uses two main pages: one that summarizes customer information, and one that allows the user to order products. You want to ensure that the customer only sees the information page once, regardless of how many transactions they enter. In order to do this, the webclass must be able to determine whether the user has seen the customer information when it processes each new order. You can do this by using the Session object to retain information about where the user has already been.

You use the Application object to store information about multiple users. For example, suppose you want to keep track of the number of users who access a webclass and access that statistic online. You can do this by storing a count in the Application object and incrementing it each time a user accesses a part of the webclass.

For More Information   See "State Management in IIS Applications" for a code example of storing state in the Session and Application objects.

The ASP Server Object

You use the Server object to create objects and determine server-specific properties that might influence the webclass's processing. For example, suppose you have a library of business objects that are used during event processing. One of these objects must be kept alive throughout the session in order to maintain internal state. You can do this using the Server and Session objects.

In this case, you could choose to store the object as a Session property, using Server.CreateObject to create an instance of it. This is necessary so that IIS can detect the threading model of the object and optimize subsequent request processing.

The following code shows how you would do this in your event procedure:

Dim BusObj as SomeBusinessObject
Set BusObj = Server.CreateObject("OurCompany.SomeBusinessObject.1")
'Code here to call methods of the object
Set Session("BusObjInstance") = BusObj

The ASP BrowserType Object

You use the BrowserType object to determine the capabilities of the user's browser and make processing decisions based on that information. For example, suppose you have an application that contains a button called CheckInventory. This button allows the user to check available inventory before placing an order. You provide two versions of the event procedure for this button in your webclass — one using HTML and one using JavaScript. You might do this if the JavaScript procedure provides a better user experience — for example, it might not cause a page transition, where the HTML procedure would.

In this scenario, you could use the BrowserType object to determine which procedure to use by determining if the user's browser supports JavaScript. The following code shows how you would use the BrowserType object to make this choice:

Private Sub OrderForm_ProcessTag(ByVal TagName As String, TagContents As String, SendTags as Boolean)

   'If the browser supports Javascript, generate HTML to call
   'a method on an applet. If it does not, generate HTML to
   'fire an event on the webclass.

   If TagName = "WC@FORMTAGSTART" Then
      If BrowserType.javascript And BrowserType.javaapplets Then
         TagContents = "<FORM onsubmit=""JavaScriptHandler()"">"
      Else
         TagContents = "<FORM ACTION=" & URLFor(OrderForm _
         "OrderForm_Submit") & " METHOD=POST>"
      End If
   End If
   SendTags = False
End Sub