1. Computing
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
TASPObject - ASP programming with Delphi
Page 2: Creating an ASP page counter to see how many times your Delphi ASP page has been called.
 More of this Feature
• Page 1: Intro to TASPObject
• Page 3: Building a web site
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Internet programming
• Using components
• CGI/ISAPI articles

   Mmmmm….Gerrrrrbil…..Er, I mean, uh, that’s a lot of work just to get an ASP page up!
Well, it is in a way, but for very large projects, this is the best way to go. Why? Well, here is my argument in favor and against using the TASPObject to do everything:

For:
a) You have access to the full power of Delphi and the full power of ASP all within the same COM object. This is important because you can do all of your ASP coding from this one COM object as we shall see in a future tutorial. You don’t need any other ASP’s besides this one! It know it’s hard to believe, but I’ll show you why in the next tutorial.
b) You are using your native language to do lower level file access, etc.
c) Using a single COM object as the entry point for a website will substantially speed up the website because the ASP will not have to rely on stupid COM objects like the FileSystemObject. This means less marshaling which means faster processing!
d) If you so desire, you could send your entire website to someone else in the form of a single .DLL and that simple .ASP that they just register on their machine. Boom. You are done!

Against:
a) Code cannot be maintained by those who do not know Delphi.
b) You will still have to program HTML and scripts into your COM object that gets sent back to the browser in the object responses. It’s not easy to track down what line a browser chokes on when you are using a COM object to do the responding.
c) It’s much faster, in the beginning, to start writing your ASP out in traditional VBScript and/or Javascript.

As you can probably tell, I’m more for using it than not using it. The ingenious minds at Borland have given us woeful developers a sweet tool to use for web RAD and I think we should exploit it for everything it’s got. Anyways, if you think that was neat and interesting, we are now about to dabble our feet into something truly cool. Unfortunately, this example requires that you have at least Windows 2000 installed on your system. It will not work with Windows 95/98 using the current version of PWS.

Before we start dabbling though, we will need to make sure you are set up to do this correctly. We are not going to be using DcomCnfg.exe to do DCOM in this lesson. We are using it to allow us to alter our out-of-process servers identity to allow us to do something that you probably haven’t read about or seen before. We are going to create a real application using Delphi that incorporates the TASPObject. After you have added some functionality to your TASPObject I will have you drop a label on your form that reflects how many times your ASP has been called. This label will be updated in real time on your screen. If you still don’t believe me, let me show you how!

1) Open Delphi
2) Click File.
3) Click the New Application menu option.
4) Again click File|New….
5) Select the ActiveX tab and click the Active Server Object icon.
6) When the New Active Server Object window pops up, type in AppASPObject then click OK. Leave the rest of the settings the same.
7) When the type library appears, change the name of the library to be AppASPLibrary.
8) Create a new method in the type library called SendAResponse. Your type library should look like the image below

project1.tlb

9) Click the Refresh icon on the type library editor form.
10) Ok. Now we need to save all of our files. Save them using the following names:
   i. Unit1.pas should be saved with the name MainUnit.Pas
   ii. Unit2.pas should be saved with the name AppASPObjectUnit.Pas.
   iii. Save your project as MyWebApp. This will be the name of the executable when you compile it.
11) Now go to your MainUnit.Pas, the one that contains the implementation of your form.
   i. Add a variable called aValue that is of integer type under the public class member.
   ii. Drop a TLabel component on your form.
   iii. Drop a TTimer component on your form.
      1. Click the Proprties tab in the object inspector and set the timer’s interval to update every 250 milliseconds.
      2. Click the Events tab in the object inspector and double click on the OnTimer event entry box.
      3. Add the following code to the Tform1.Timer1Timer event that gets created for you:
             Form1.Label1.Caption := IntToStr(AValue);
             Form1.Refresh;
12) At this point, your form should look like this….

form1

13) And your code should look like this in MainUnit.Pas:

unit MainUnit; 
interface 
uses 
  Windows, Messages, SysUtils, Classes, 
	Graphics, Controls, Forms, Dialogs, 
  ExtCtrls, StdCtrls; 
type 
  TForm1 = class(TForm) 
    Label1: TLabel; 
    Timer1: TTimer; 
    procedure Timer1Timer(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
    AValue : Integer; 
  end; 

var 
  Form1: TForm1; 

implementation 
{$R *.DFM} 
procedure TForm1.Timer1Timer(Sender: TObject); 
begin 
  Form1.Label1.Caption := IntToStr(AValue); 
  Form1.Refresh; 
end; 
end. 

14) Now switch over to AppASPObjectUnit.Pas, and change the 2nd uses clause to include your MainUnit.Pas. It should look like this: uses ComServ, MainUnit;
15) Find procedure TAppASPObject.SendAResponse and add the following code:
   Response.Write('Incrementing the value!');
   Form1.AValue := Form1.AValue + 1;
16) Now go to the ASP file that Delphi automatically generated for you and change it so that it looks like the following highlighted in blue:

<HTML> 
  <BODY> 
  <TITLE> Testing Delphi ASP </TITLE> 
  <CENTER> 
    <H3> You should see the results of your
		Delphi Active Server method below </H3> 
  </CENTER> 
  <HR> 
    <% Set DelphiASPObj = Server.CreateObject
		         ("AppASPLibrary.AppASPObject") 
       DelphiASPObj.SendAResponse 
    %> 
  <HR> 
  </BODY> 
</HTML> 

17) Now compile it and run it! After it has run, close the program. This is how you can register an out-of-process server. If this doesn’t seem to work, drop to a command prompt and run your program again with the /regserver extension (I.E. MyWebApp.exe /regserver) and that should do the trick. You must do this step or your object won’t appear in the list that dcomcnfg gives you!
18) Now open a command prompt window (or DOS prompt if you’re an old-timer like me) and type dcomcnfg.exe. (This should already be installed as mentioned earlier)
   i. Find your object in the list. It will be named AppASPObject.
   ii.

dcom conf. properties


   iii. After you have selected your object, click the Properties button.
   iv. Select the Identity tab and set your objects identity to interactive user.
   v. Click the Apply button.

AppASPObject properties


   vi. Now click on the Security tab. Change your Access Permissions and Launch Permissions to use Custom permissions by clicking the radio buttons. It should look like the following.

ASPAPPObject Security properties


   vii. Now click the Access Permissions “EDIT...” button.
   viii. Make sure that permissions include both NETWORK and SYSTEM. If they do not, click the ADD button and select it from the drop down list. When finished, click the OK button.
   ix. Now click the Launch Permission “EDIT…” button. Once again make sure that NETWORK and SYSTEM are included.

Registry Value Permisions


   x. When finished, click the APPLY button and then click the OK button!.

19) Now you need to save your ASP file into a directory that you can browse to. The directory I have been using throughout these tutorials is c:\asplessons so go ahead and save it there.
20) Run MyWebApp.Exe
21) Open up your browser and browse to your asp page using the localhost like we did in previous lessons. (I.E. http://localhost/WebAppTest.ASP)
22) If all went according to plan, you should see the number on your form increment every time your click the refresh button on your browser! Is that cool or what?

Click here to download the code for this: Lesson6_b.zip

   Well, that is pretty cool, but how does it work?
This has been a pretty long lesson so I don’t want to talk about it too in-depth so I’ll summarize it. You should already know that when you create an out-of-process server, that objects contained within that server are accessible whether that server is running or not. When we did the Server.CreateObject in the ASP page, we told it to basically run MyWebApp.Exe to get its object. This is similar to automating MSWord or MSExcel. Anyway, if you do not change it’s identity using dcomcnfg and it stays as the launching user, then it will load a separate out-of-proc server every time the user hits the refresh button! Why does it do this? The answer is simple, really.

The launching user is not you. The launching user is the client using the browser. When they go through IIS to get to your ASP, it uses a guest account to log into your system. It is this guest account that is executing your ASP, and thus your out-of-proc server. This is called impersonation where the guest account acts as if it’s a user logged into your system. When you change the identity to the interactive user, you are telling your out-of-proc server that every time it is run that it needs to use the identify of the person who is currently logged into the system. In this case, that would be you. Thus, once you have the server running, it knows it can get all of its objects from the currently running server. Wala! You get to see updates in real time! For a fun test, go ahead and change it back to Launching user and see what happens (you shouldn’t see any real time updates).

On a side note, I’d like to mention that the code we just created about is not thread safe. I should have theoretically put a critical section around my changes to the AValue variable in AppASPObjectUnit.Pas. If you remember from previous lessons, you can have multiple users access your ASP at the same time so you will need to put a critical section around all such code that affects a variable that is contained in your main form.

   Summary
Today we learned how to use the TASPObject at the simplest level. There is actually quite a bit of stuff you can do with this object and we will explore more during the next tutorial. We also learned more about identities and how to impregnate a regular Delphi application with the TASPObject. Pretty exciting stuff! Okay, now for the homework.

   Homework
Let’s see…how shall I punish you this time? Hmmm. Ok. I got it. I want you to:

a) Build a simple website that asks the user to guess what number the computer is thinking of.
b) The TASPObject must:
  a. Send the HTML back to the client that builds the form for the number entry
  b. Process the users response and send back an answer if they were right or wrong.

Click here to download the code for this: Lesson6HomeWork.zip

This should give you some experience with the request/response mechanism using the TASPObject. There is a thing or two I will say to help you along the way. You will need to use the Request interface and you may want to use the IStringList that comes bundled with the ASPTlb.pas in order to get your form data out of the it…that is the only hint I’ll give you…

Happy coding!

Next page > Discussing a complete web architecture using the TASPObject object > Page 1, 2, 3

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: · NEXT ARTICLE: Windows XP Manifest in Delphi.
Find out how you can include the manifest into a Delphi project to allow your application to share the same look and feel of Windows XP.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

©2013 About.com. All rights reserved.