
A seguir apresentamos dois exemplos utilizando a linguagem OPUSWEB. O primeiro exemplo gera o programa teste1 de um aplicativo CGI. O segundo exemplo gera a DLL teste2.dll de um aplicativo ISAPI.
Os dois exemplos apresentam diferenças apenas nas duas primeiras linhas de código e fazem, exatamente, as mesmas coisas, a saber:
O esquema do banco usado nestes exemplos é o seguinte:
Banco EXEMPLO 1 arqrecup
nome: PESSOA E
nome(0) U20
idade N3
$web,nolib
prog teste1
gw_nome=rep("?",20)
gw_cont=""
gw_ida="***"
content=html_var("CONTENT_LENGTH")
if val(content) > 0
html_read()
html_get("in_nome",gw_nome)
html_get("in_cont",gw_cont)
database EXEMPLO 1 a 2
use PESSOA
find gw_nome
if eof()
gw_ida="***"
else
gw_ida=str(idade,3)
endif
endif && se Content_length>0
html_init()
html_head()
BeginHtml
<html><head><title>Teste OpusWEB</title></head>
<body>
<h2>Demonstração OpusWEB com CGI</h2>
<form method="post" action="/cgi-bin/teste1">
Informe o nome:
<input type=text size=20 name="in_nome" value="#">
<input type=submit name="in_cont" value="Continua ">
</form>
<br><b>Resultado: Nome=# <==> Idade=#</b>
</body></html>
EndHtml
html_write("")
return
$dll=teste2.dll
proc Http
gw_nome=rep("?",20)
gw_cont=""
gw_ida="***"
content=html_var("CONTENT_LENGTH")
if val(content) > 0
html_read()
html_get("in_nome",gw_nome)
html_get("in_cont",gw_cont)
database EXEMPLO 1 a 2
use PESSOA
find gw_nome
if eof()
gw_ida="***"
else
gw_ida=str(idade,3)
endif
endif && se Content_length>0
html_init()
html_head()
BeginHtml
<html><head><title>Teste OpusWEB</title></head>
<body>
<h2>Demonstração OpusWEB com ISAPI</h2>
<form method="post" action="/scripts/teste2.dll">
Informe o nome:
<input type=text size=20 name="in_nome" value="#">
<input type=submit name="in_cont" value="Continua ">
</form>
<br><b>Resultado: Nome=# <==> Idade=#</b>
</body></html>
EndHtml
html_write("")
return
O programa OpusWEB a seguir apresenta um aplicativo Internet CGI cujo objetivo é manter uma agenda telefônica, devendo ser compilado com o comando OPUS em ambiente UNIX e disponibilizado num Servidor WEB com suporte para CGI, por exemplo, o Apache.
Se você quiser transformá-lo em uma aplicação ISAPI, basta modificar as duas primeiras linhas do programa fonte, conforme a seguir, e compilá-lo com o comando Opuswin em ambiente Windows e disponibilizados num Servidor WEB com suporte para ISAPI, por exemplo, PWS (Personal Web Server), IIS (NT/200), Apache for Windows e outros que você pode obter gratuitamente na Internet.
Para construir uma DLL ISAPI, com o nome de idemow1.dll (ou idemow1.isa), utilizando o mesmo fonte, modifique assim as duas primeiras linhas;
$dll=idemow1.dll (ou $dll=idemow1.isa)
Proc http
O Banco de Dados deste aplicativo é o seguinte:
banco agenda 1
nome: contatos E
registro:
ag_nome(*) U48
ag_fone U72
ag_email U120
ag_obs O4
O código fonte do programa é o seguinte:
$web
prog idemox1
*private dynamic gw_obs,temp1
private gw_obs(361),temp1(361)
mens1 = ""
lmsg = .f. && mensagem a exibir ?
lget = .f. && faz html_get ?
lprim = .f. && se for a primeira chamada do programa
lresult = .f. && resultado da pesquisa
Database /home/openserv/db/agenda 1 a 2
**********************************************************
*** inicializa variáveis de trabalho - campos do FORM ***
**********************************************************
gw_nome, gw_fone, gw_email, gw_obs = ""
gw_pes, gw_inc, gw_exc, gw_alt, gw_res = ""
gw_pro, gw_ant, gw_pri, gw_ult = ""
*******************************************************
*** Obtêm variáveis de ambiente (server variables) ***
*******************************************************
content=html_var("CONTENT_LENGTH")
nome_prog=html_var("SCRIPT_NAME")
gw_qry=html_var("QUERY_STRING")
gw_qry=strtran(gw_qry,"%20"," ") && retira marcas MIME
gw_qry=strtran(gw_qry,"+"," ") && retira marcas MIME
**********************************************************
*** Se houve campos digitados pelo usuário ... ***
*** ... no FORM apresentado na tela do Browser ... ***
*** ... então são obtidos e processados esses campos. ***
**********************************************************
cl=val(content)
if (cl > 0)
lget = .t.
else
lprim = .t.
endif
do while (lget)
lget = .f.
html_read()
*******************************
*** obtém campos do FORM ***
*******************************
html_get("in_nome",gw_nome)
html_get("in_fone",gw_fone)
html_get("in_email",gw_email)
html_get("in_obs",gw_obs)
html_get("in_pes",gw_pes)
html_get("in_alt",gw_alt)
html_get("in_inc",gw_inc)
html_get("in_exc",gw_exc)
html_get("in_res",gw_res)
html_get("in_pro",gw_pro)
html_get("in_ant",gw_ant)
html_get("in_pri",gw_pri)
html_get("in_ult",gw_ult)
******************************************
*** decide o que deva ser feito ... **
*** ... de acordo com o botão clicado **
******************************************
if !empty(gw_res)
gw_nome, gw_fone, gw_email, gw_obs = ""
exit
endif
select a
use contatos
If !empty(gw_pes)
locate
Do While found()
lnome, lfone, lemail = .t.
lresult = .f.
If !empty(gw_nome)
if !(gw_nome $ ag_nome)
lnome = .f.
endif
Endif
If !empty(gw_fone)
if !(gw_fone $ ag_fone)
lfone = .f.
endif
Endif
If !empty(gw_email)
If !(gw_email $ ag_email)
lemail = .f.
endif
Endif
If (lnome .and. lfone .and. lemail)
lresult = .t. && pesquisa "and": se achou... vai mostrar
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
exit
endif
continue
Enddo
if !lresult
mens1 = "Nenhum registro encontrado na agenda ..."
lmsg = .t.
exit
endif
Endif
if !empty(gw_pri)
locate
if eof()
mens1 = "Registro não encontrado na agenda! ..."
lmsg = .t.
exit
endif
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
exit
endif
if !empty(gw_ult)
locate last
if eof()
mens1 = "Registro não encontrado na agenda! ..."
lmsg = .t.
exit
endif
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
exit
endif
if empty(gw_nome)
lmsg = .t.
mens1 = "Faltam informações obrigatórias ..."
exit
endif
if !empty(gw_exc)
find gw_nome
if !eof( )
delete
mens1 = "Registro excluído da agenda ..."
else
mens1 = "Nome não encontrado na agenda ..."
endif
lmsg = .t.
exit
endif
if !empty(gw_pro)
locate start gw_qry
continue
if eof()
mens1 = "Chegamos ao fim da agenda! ..."
lmsg = .t.
exit
endif
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
exit
endif
if !empty(gw_ant)
locate start gw_qry
reverse
continue
if eof()
mens1 = "Chegamos ao início da agenda! ..."
lmsg = .t.
exit
endif
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
exit
endif
if len(gw_obs) > 360
mens1 = 'Tamanho excedido no campo observações ...'
lmsg = .t.
exit
endif
if !empty(gw_inc)
find gw_nome
if !eof( )
mens1 = "Registro já existe na agenda ..."
else
replace ag_nome with gw_nome,;
ag_fone with gw_fone,;
ag_email with gw_email
insert
if ! memoput (ag_obs,gw_obs)
mens1 = "Falhou inclusão de registro na agenda ..."
else
mens1 = "Registro incluído na agenda ..."
endif
endif
lmsg = .t.
exit
endif
if !empty(gw_alt)
find gw_nome
if eof( )
mens1 = "Registro não existe na agenda ..."
else
replace ag_fone with gw_fone,;
ag_email with gw_email
change
if ! memoput (ag_obs,gw_obs)
mens1 = "Falhou alteração de registro na agenda ..."
else
mens1 = "Registro alterado na agenda ..."
endif
endif
lmsg = .t.
exit
endif
enddo && fim de "do while (lget)"
if lprim && se primeira vez, mostra o primeiro registro da agenda
select a
use contatos
locate
if !eof()
gw_nome = ag_nome
gw_fone = ag_fone
gw_email = ag_email
if ! memoget (ag_obs,gw_obs)
mens1 = "Falhou leitura de registro na agenda ..."
lmsg = .t.
endif
endif
endif
**********************************************
*** Build response to web browser ***
**********************************************
html_init("")
html_head()
if !empty(gw_nome)
gx_nome=trim(gw_nome)
gx_nome=strtran(gx_nome," ","%20") && coloca MIME (Netscape!!!)
nome_prog = nome_prog + "?" + gx_nome
endif
BeginHtml
<html><head>
<title>OpenBASE - OpusWEB - Programa para demonstração</title>
</head>
<body bgcolor="#FFFFDD">
<form method="post" action=#>
EndHtml
if lmsg
lmsg = .f.
srt_mens = ""
str_mens = trim(mens1)
html_put('Aviso: <font color="#0000ff"><b>')
html_put(str_mens)
html_put('</b></font><br>'+chr(10))
endif
BeginHtml
<div>
<h2 align=center color='#FF0000'>Agenda telefônica</h2>
<hr color='#0000FF' size=6 noshade width=100%>
</div
<div bgcolor="#CCCCFF">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td align="right" width="30%"><b>Nome:</b></td>
<td align="left" width="70%">
<input type="text" name="in_nome" size="30" maxlength="48"
value=#></td>
</tr>
<tr>
<td align="right" width="30%"><b>Fone(s):</b></td>
<td align="left" width="70%">
<input type="text" name="in_fone" size="30" maxlength="72"
value=#></td>
</tr>
<tr>
<td align="right" width="30%"><b>Email(s):</b></td>
<td align="left" width="70%">
<input type="text" name="in_email" size="60" maxlength="120"
value=#></td>
</tr>
<tr>
<td align="right" width="30%"><b>Observações:</b></td>
<td align="left" width="70%">
<textarea name="in_obs" cols="72" rows="5">@+@trim(gw_obs)@+@
</textarea></td>
</tr>
</table>
</div>
<div>
<hr color='#0000FF' size=6 noshade width=100%>
</div>
<div>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td colspan="2" align="center" bgcolor="#ffffca">
<input type="submit" name="in_pes" value=" Pesquisar ">
<input type="submit" name="in_alt" value=" Alterar ">
<input type="submit" name="in_inc" value=" Incluir ">
<input type="submit" name="in_exc" value=" Excluir ">
<input type="submit" name="in_res" value=" Limpar ">
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#ffffca">
<input type="submit" name="in_pro" value=" Próximo ">
<input type="submit" name="in_ant" value=" Anterior ">
<input type="submit" name="in_pri" value=" Primeiro ">
<input type="submit" name="in_ult" value=" Último ">
</td>
</tr>
</table>
</div>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td colspan="2" align="center">
<font color="#ff0000" face="Arial, Helvetica, sans-serif" size="1">
© OpenBASE - OpusWEB. Todos os direitos reservados.</font>
</td>
</tr>
</table>
</body></html>
EndHtml
html_write("")
Return
