Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Stardeveloper on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (38)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : COM : ASP Intrinsic Objects from within COM Component
 

ASP Intrinsic Objects from within COM Component
by Faisal Khan.

As we saw in our tutorial ASP Component in Visual Basic it is very easy and natural for an ASP developer to create and use components using Visual Basic. I will build on that tutorial by adding more functionality to it. This time we will also make available five ASP Intrinsic Objects i.e. Request, Response, Server, Session, and Application, to our component. It is very important to make use of ASP Intrinsic Objects from within COM and as you become experience you will use it time and time again.

Before beginning I will assume that you have read ASP Component in Visual Basic tutorial and are comfortable with creating simplest of COM component using Visual Basic. If you are not then I recommend having a look at that tutorial because it covers the basics of creating COM component and after reading it you will be able to better understand the following tutorial.

Our Objective
Our objective in this tutorial is simply to make five ASP Intrinsic Objects available to our COM component. We will then use them as easily as we use them in our ASP pages. In this tutorial we will only use Request Object but the procedure for using other ASP Objects is the same.

Our component this time will consist of two methods, IsIE and IsNS. IsIE will check to see if the browser used by the client is Microsoft Internet Explorer or not. If it is Microsoft Internet Explorer then it will return true and otherwise false. Same is the case with IsNS which will detect Netscape Navigator, returning true it browser is Netscape and false if it isn't.

Our component will only work with IIS4 / PWS 4 or above.

Let's Start :

  • Step 1 : Start Microsoft Visual Basic.
  • Step 2 : From the 'New Project' dialogue box select 'ActiveX DLL' and hit 'Open'.
  • Step 3 : Change Project Name to 'Stardeveloper' and Class Name to 'CheckBrowser' so that it looks like following :
    Stardeveloper = CheckBrowser
    Stardeveloper = CheckBrowser
  • Step 4 : Now add the following line on the top of CheckBorwser(Code)' window :
    		Option Explicit
    It ensures that our variables are defined before we use them.
  • Step 5 : Now add the following code below the above line :
    Private ObjectContext As MTxAS.ObjectContext
    Private Request As ASPTypeLibrary.Request
    Private Response As ASPTypeLibrary.Response
    Private Session As ASPTypeLibrary.Session
    Private Application As ASPTypeLibrary.Application
    Private Server As ASPTypeLibrary.Server
    These six variables will later hold six Objects of their types. Note the most important one is 'ObjectContext' of type 'MTxAS.ObjectContext'. This is the one we will use to create five ASP Intrinsic Objects.
  • Step 6 : Add following code below the above code :
    Private Sub Class_Initialize()
    	Set ObjectContext = GetObjectContext()
    	Set Response = ObjectContext("Response")
    	Set Request = ObjectContext("Request")
    	Set Session = ObjectContext("Session")
    	Set Application = ObjectContext("Application")
    	Set Server = ObjectContext("Server")  
    End Sub
    This Sub routine is for the component's own private use and we will not call it from our ASP code. But it is very important from component's point of view. When Server instantiates our component, it calls this Sub routine before creating the Object. So we take use of it and let it create the five ASP Objects and the very important ObjectContext Object. The six variables we had created earlier are now Objects. We can now use the five ASP Objects like we used them in ASP pages.
  • Step 7 : Lets now add our first Public Function, IsIE. Add the following code below the above code :
    Public Function IsIE() As Boolean
    	Dim var As String
    	var = Request.ServerVariables("HTTP_USER_AGENT")
    		If InStr(1, var, "msie", vbTextCompare) Then
    			IsIE = True
    		Else
    			IsIE = False
    		End If
    	End Function
    If you know a bit of ASP then you won't find any problem understanding the code used here. IsIE returns a boolean ( True / False ) value depending on the condition "msie" exists in the HTTP_USER_AGENT string from the Request.ServerVariables collection. Said simply IsIE will return True if client browser is Microsoft Internet Explorer and False if isn't.
  • Step 8 : Now add our second Public Function, IsNS. Add the following code below the above code :
    Public Function IsNS() As Boolean
    	Dim var As String
    		var = Request.ServerVariables("HTTP_USER_AGENT")
    		If InStr(1, var, "Mozilla", vbTextCompare)
    			And InStr(1, var, _
    				"compatible;", vbTextCompare) = 0 Then
    			IsNS = True
    		Else
    			IsNS = False
    		End If
    	End Function
    Almost similar to IsIE, IsNS checks for the presence of "Mozilla" in the HTTP_USER_AGENT string. Since "Mozilla" is also present in Microsoft Internet Explorer, we also check for the presence of "compatible;" in the HTTP_USER_AGENT string. "compatible;" is present only in Microsoft Internet Explorer, so it's absence means that the browser is Netscape Compatible ( Navigator or Communicator ). IsNS returns True if browser is Netscape Compatible and False if it isn't.
  • Step 9 : Ok we are almost done. But let's create a Private Sub routine to destroy the Objects created by the Class_Initialize() Sub and free Server's resources. Add the following code below the above code :
    Private Sub Class_Terminate()
    	Set ObjectContext = Nothing
    	Set Request = Nothing
    	Set Response = Nothing
    	Set Session = Nothing
    	Set Application = Nothing
    	Set Server = Nothing
    End Sub

    Like Class_Initialize which is called by Server when creating an instance of our component, Class_Terminate Sub is called when Server destroys the component Object. We use it to destory the six Objects we created in the component. Note Class_Initialize() and Class_Terminate() Subs are there to help you in creating the Objects and later freeing the resources, they are NOT required to make use of ASP Intrinsic Objects. Had we not used them this way, we had to create a Public method and call that from our ASP pages to create the Objects explicitly which would have been nothing but an overhead.

    You can add as many Public methods making use of ASP Intrinsic Objects as you like between the Class_Initialize() and Class_Terminate() Subs. We will stick with two because our aim is just to show you how to call them and make use of them, you can later add according to your own needs.

  • Step 10 : Go to the Properties of 'CheckBrowser' Class Module and edit the 'Properties - CheckBrowser' window so that 'MTSTransactionMode' is set to '1 - NoTransactions'.
    'Properties - CheckBrowser'
    'Properties - CheckBrowser'
    This step registers the component with MTS and although not transactional, does make ObjectContext available where necessary.
  • Step 11 : Go to Project -> References. 'References - Stardeveloper.vbp' dialogue window opens up. Checked References are available to be used in our Project. By default 4 of them will already be checked. To make use of ASP Objects we have to add two more references in our Project, 'Microsoft Active Server Pages Object Library' and 'Microsoft Transaction Server Type Library'. Scroll down to find the above two references and click them so that they get checked. Then hit 'Ok'.

 ( 1 Remaining ) Next

Comments/Questions ( Threads: 2, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. If 'MTS Type Library' Reference Is Not Available, use COM+ Services Type Library Instead
  2. 'Microsoft Transaction Server Type Library' Reference Is Not Available. Where can I find it?

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.