Introduction
Web program development poses great challenges to development tools. Faced with the numerous needs of users, many companies have launched a large number of development platforms: those for developing desktop applications and C/S programs, those for developing middleware, and those for developing Web servers. This results in programmers facing a large number of tools with no way to start.
DELPHI is different from other development tools because it is an open system. As long as some controls are used flexibly, various types of systems can be developed, whether N-Tie programs, multi-threaded programs, distributed computing programs (including DCOM and CORBAR), TCP programs, Web programs, ActiveX, middleware, push programs, and you can even use it to write assembly programs.
DELPHI cleverly encapsulates ISAPI/NSAPI/CGI/WCGI, etc. into a class. Users can get different systems as long as they select the compilation results during compilation.
In DELPHI4, InPRise has further strengthened its support for Web program development and can develop better and stronger systems. The following are several common problems in developing web applications for your reference. If there is no special statement, it means that the program is running under DELPHI 4.
How to return an image from a Web Server application?
Web Server Application can not only generate complex page documents, but also return different images according to user requests. Of course, there is a simpler method. Depending on the input parameters, the <img src...> tag also points to different URL addresses. We don't use this method here, but use the DLL to return the image.
Of course, you must first create a page container (page producer) with the following content:
〈html〉
〈body〉This is a test〈BR〉〈img src=″/scripts/mydll.dll/picture″〉〈/body〉
〈/html〉
Next, we set the action event corresponding to PathInfo and return the image result. The source code is as follows:
(Note: JPEG unit must be included in the unit declaration)
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
Jpg: TJpegImage;
S: TMemoryStream;
begin
Jpg := TJpegImage.Create;
try
Jpg.LoadFromFile(′test′);
S := TMemoryStream.Create;
try
Jpg.SaveToStream(S);
S.Position := 0;
Response.ContentType := ′image/jpeg′;
Response.ContentStream := S;
//Must complete before stream is released
Response.SendResponse;
finally
S.Free;
end;
finally
Jpg.Free;
end;
end;
In fact, this method is safer and more flexible than the simple method mentioned above. It can be used flexibly in some places, and slight modifications based on it can produce effects that are difficult to achieve with general development tools.
How to use native access driver with ISAPI/NSAPI dynamic link library (DLL)?
This is because DAO 3.0 or DAO 3.5 is a so-called thread-safe (thread-safe program), and a Web server (such as IIS) will generate multiple threads according to user requests, and also lists the DLL corresponding to ISAPI as a thread. At this time, ISAPI will notify DAO through BDE, telling it that it does not comply with thread safety regulations.
There are many solutions. If you must access the Access 95/97 library, you can access it through ODBC. ODBC does not go through DAO and is also a thread-safe program. In addition, there are some third-party control sets through which Access 95/97 can be accessed directly from BDE, which is more efficient.
The user accesses the ISAPI DLL on my web server, but reports: "Invalid filename", although the file does exist. By the way, my database is on a Novell server. Why is this?
You have not configured the drive path mapping (MAPING) corresponding to your IUSR_XXX account. Because Novell does not use FAT, path mapping must be added manually. Of course, it can be made into a boot login script. Keep in mind that if you are running IIS as a Web server and you are involved with Novell, either as a file server or database server, you must define the path mapping.
"Invalid configuration parameter for alias {alias_name}" (invalid alias configuration), such an error occurs when I set up an ODBC DSN and access the ISAPI/NSAPI server through it.
If you want to create an ODBC alias for the access user (IIS user), then be careful to create a SYSTEM DSN (system DNS) instead of creating a "user DNS", although "user DNS" is the default setting.
How to obtain the name and IP address of the client (accessing machine)?
It is very easy to implement this function using TCP control. Select a TCP control from the Internet page and get what you need directly:
Memo1.Lines.Add(TCP1.LocalHostName);
Memo1.Lines.Add(TCP1.LocalIp);
Of course, if you don't want to do this, there are more complicated ways:
uses Winsock;
procedure TForm1.FormCreate(Sender: TObject);
var
wVersionRequested : Word;
wsaData : TWSAData;
begin
{Create WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
p: PHostEnt;
s : array[0..128] of char;
p2: pchar;
begin
{get computer name}
GetHostName(@s,128);
p:=GetHostByName(@s);
Memo1.Lines.Add(p^.h_Name);
{Get machine IP address}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
Memo1.Lines.Add(p2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{Release WinSock}
WSACleanup;
end;
This is an independent unit called WINSOCK, which you can embed directly into your program.
Why can't I create a true multi-threaded DLL in DELPHI 3?
Although the ISAPI DLL wizard in DELPHI3 has generated a lot of code for creating multi-threaded DLLs, there is still a serious flaw: it does not declare that this application is a multi-threaded program. So you need to add a sentence:
IsMultiThread := TRUE;
Place this sentence at the beginning of the begin-end block of the DPR program, making it the first sentence.
How do I know if I am currently connected to the Internet?
The simplest way is to use a TCP component to get your current IP, and determine whether you are connected to the Internet by judging the IP. For example:
if TCP1.LocalIp = '0.0.0.0' then
ShowMessage('Not currently connected to the Internet!');
It should be noted that because there is no essential difference between the Internet and Intranet, it is generally impossible to determine whether it is connected to the Internet or just connected to the Intranet. certainly. You can also add a PING component to PING a relatively stable and fast site. If it is connected, it means you have access to the Internet. However, this method is not very versatile.
How to print a web page?
You can choose to use the AutoPrint method of the HTML control. For example:
uses Printers;
procedure TForm1.Button1Click(Sender: TObject);
var
OldCur: TCursor;
begin
OldCur := Screen.Cursor;
with Printer do
begin
BeginDoc;
HTML1.AutoPrint(handle);
Title := HTML1.URL;
EndDoc;
end;
Screen.Cursor := OldCur;
end;
In addition, you can also use its PrintPage method. However, I recommend you to use AutoPrint, because this control is more flexible and can filter some content you do not want to print.
Author's Blog: http://blog.csdn.net/zou5655/