Thursday, October 05, 2006

 

Web software in Pascal?


In 1997, i and a partner where the owners of a small graphic design and editorial studio working with 133mhz - Win95 PCs and a Mac Performa 6200 (90 mhz). In that time, the usual type of job a customer could request was the design of a leaflet or printed magazine, eventually a customer could call asking for the design of a static (html only) web page.

Later that year, the web started to get traction between small business here in Argentina and an idea came to our mind, bring people the possibility to check the price of any product before buy, all using the web. What a great idea!, the only problem was that we didn't know how to create dynamic web pages such those of (in that time) Viaweb (now Yahoo Store). Since then, i started to learn everything i could with the objective to generate dynamic web pages.

The first tool i used was IDC (Internet Database Connector), a small utility created by Microsoft for the IIS (Internet Information Server) that allows users to embed a database query into an HTML page. Later came ASP with a lot more features, i remember used it to create the firtst elshopping.com, now defunct (it makes me sad). Then cames PHP, way faster than ASP and multiplatform and Java with JSP. Now we must add Python and frameworks.

With that background i thought web development meant scripting languages, in fact, i'd repeatedly read that CGI isn't a good development choice because after each request, a new fork is created turning the proccess very inefficient. Well, this site shows that the inneficient CGI is more efficient than scripting languages (many times faster).

After that introduction, i'll show you an open source library that allows CGI creation using FreePascal. It's name is Pascal Server Pages (PSP for short).

Many times i wanted to create dynamic web pages in FreePascal, and must admit, i tought Pascal Server Pages was an emulation of ASP, but for Pascal language, and did't pay much attention to it. But last week, i found PSP fits very well into the kind of software i like to create, fast-efficient and easy to improve.

You can start reading Vladimirs Sibirov's introduction to PSP to learn more.

Well, we have arrived to the end of this post, and i know you want to see some code. The example below is a neat trick that allows you to embed images into the HTML generated by PSP.

To compile this program, you must download PSP from http://sourceforge.net/projects/pascal-webdev/ and make sure your compiler can find the base64enc unit included in the PSP source tree. Also you'll need a web server capable of executing CGI files (i preffer Apache) and copy the resulting file (embedimage.exe) to it.
program embedimage;

{$mode objfpc}{$H+}

uses
SysUtils,
Classes,
base64enc,
pwu;

var
lImage: TMemoryStream;
lString: PChar;
lStrImage: string;

begin
WebWriteLn('<html>');

WebWriteLn('Hello, this is an embedded image<hr>');

(* Load the image from disk as a stream *)
lImage := TMemoryStream.Create;
lImage.LoadFromFile('image1.jpg');

(* Convert the stream to a Base64 encoded string *)
GetMem(lString, lImage.Size);
lImage.Read(lString^, lImage.Size);
SetString(lStrImage, lString, lImage.Size);

WebWriteLn('</html>');
WebWriteLn('<img src="data:image/jpg;base64,'
+ base64_encode(lStrImage) + '">');
end.


This page is powered by Blogger. Isn't yours?