Professional Documents
Culture Documents
DE CIENCIAS E INGENIERIA
ESCUELA PROFESIONAL
INGENIERIA INFORMATICA
Ejercicios de Asp
HUANCAYO _ PERÚ
2006
1. Escribir texto en el cliente
<html>
<body>
<%
response.write"¡Hola Mundo!"
%>
</body>
</html>
2. Formatear el texto
<html>
<body>
<%
response.write("<h2>¡Hola Mundo!<br> Esta sentencia usa etiquetas HTML para formatear el texto
</h2>")
%>
</body>
</html>
<%
Dim nombre
nombre="Juan Soldado"
</body>
</html>
<%
Dim amigos(5)
amigos(0) = "Angel"
amigos(1) = "Luis"
amigos(2) = "Josito"
amigos(3) = "Pepe"
amigos(4) = "Ignacio"
amigos(5) = "Enrique"
For i = 0 to 5
response.write(amigos(i) & "<br>")
Next
%>
</body>
</html>
5. Mi primer bucle
<html>
<body>
<%
Dim i
for i = 1 to 6
response.write("<h" & i & ">Esta es la cabecera " & i & "</h" & i & ">")
next
%>
</body>
</html>
6. ¿Qué hora es?
html>
<body>
<%
Dim h
h = hour(now())
response.write("<p>" & now())
response.write(" (Hora de Madrid (España)) </p>")
If h < 12 then
response.write("¡Buenos Dias!")
else
response.write("¡Buenas tardes!")
end if
%>
</body>
</html>
7. Dia y Horas
<html>
<body>
</body>
</html>
8. Nombre del dia
<html>
<body>
<p>
VBScripts' funcion <b>WeekdayName</b> para sacar el nombre del dia de la semana:
</p>
<%
response.Write(WeekDayName(1))
response.Write("<br>")
response.Write(WeekDayName(2))
%>
<p>Abreviatura del dia de la semana:</p>
<%
response.Write(WeekDayName(1,true))
response.Write("<br>")
response.Write(WeekDayName(2,true))
%>
<p>Dia de la semana actual:</p>
<%
response.Write(WeekdayName(weekday(date)))
response.Write("<br>")
response.Write(WeekdayName(weekday(date), true))
%>
</body>
</html>
9. Nombre del mes
html>
<body>
<p>
VBScripts' funcion <b>MonthName</b> para sacar el nombre del mes:
</p>
<%
response.Write(MonthName(1))
response.Write("<br>")
response.Write(MonthName(2))
%>
<p>Abreviatura del mes:</p>
<%
response.Write(MonthName(1,true))
response.Write("<br>")
response.Write(MonthName(2,true))
%>
<p>mes actual:</p>
<%
response.Write(MonthName(month(date)))
response.Write("<br>")
response.Write(MonthName(month(date), true))
%>
</body>
</html>
10. ¿Qué dia y que mes es hoy?
<html>
<body>
Hoy es:
<%response.write(WeekdayName(weekday(date)))%>,
<br />
y el mes es:
<%response.write(MonthName(month(date)))%>
</body>
</html>
<p>
<%millennium=cdate("1/1/3000 00:00:00")%>
It is
<%response.write(DateDiff("yyyy", Now(), millennium))%>
años hasta el año 3000!
<br>
It is
<%response.write(DateDiff("m", Now(), millennium))%>
meses hata el año 3000!
<br>
It is
<%response.write(DateDiff("ww", Now(), millennium))%>
semanas hasta el año 3000!
<br>
It is
<%response.write(DateDiff("d", Now(), millennium))%>
dias hasta el año 3000!
<br>
It is
<%response.write(DateDiff("h", Now(), millennium))%>
horas hasta el año 3000!
<br>
It is
<%response.write(DateDiff("n", Now(), millennium))%>
minutos hasta el año 3000!
<br>
It is
<%response.write(DateDiff("s", Now(), millennium))%>
segundos hasta el año 3000!
</p>
</body>
</html>
<p>
Sintaxis de DateAdd: DateAdd(intervalo,numero,fecha). Puedes usar <b>DateAdd</b> para , por
ejemplo,calcualr la fecha de dentro de 30 dias a partir de hoy.
</p>
</body>
</html>
<%
somedate="10/30/99"
response.write(IsDate(somedate))
%>
</body>
</html>
14. Convirtiendo mayusculas y minusculas
<html>
<body>
<%
name = "Juan Soldado"
response.write(ucase(name))
response.write("<br>")
response.write(lcase(name))
%>
</body>
</html>
<%
name = " AspTutor "
response.write("visita" & name & "ahora<br />")
response.write("visita" & trim(name) & "ahora<br />")
response.write("visita" & ltrim(name) & "ahora<br />")
response.write("visita" & rtrim(name) & "ahora")
%>
</body>
</html>
<%
Texto = "En un lugar de la mancha ..."
response.write(strReverse(Texto))
%>
</body>
</html>
<%
i = 1722.6783223
j = 17.66634
response.write(Round(i))
response.write("<br>")
response.write(Round(j))
%>
</body>
</html>
<%
randomize()
response.write(rnd())
%>
</body>
</html>
<%
texto="Bienvenido a esta web"
response.write(Left(texto,7))
response.write("<br>")
response.write(Right(texto,7))
%>
</body>
</html>
<%
texto="Bienvenido a esta Web!!"
response.write(Replace(texto, "Web", "Pagina"))
%>
</body>
</html>
<%
texto="Bienvenido a AspTutor!!"
response.write(Mid(texto, 9, 2))
%>
</body>
</html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p>
El resultado del calculo es: <%call vbproc(3,4)%>
</p>
<p>
Tambien puedes llamar al procedimiento sin call
</p>
<p>
El resultado del calculo es: <%vbproc 3,4%>
</p>
</body>
</html>
<%
If Request.QueryString("fname")<>"" Then
Response.Write ("Hola " & Request.QueryString("fname") & "!")
Response.Write ("<br>¿Como estas hoy?")
End If
%>
</body>
</html>
<%
If Request.form("fname")<>"" Then
Response.Write ("Hola " & Request.form("fname") & "!")
Response.Write ("<br>¿Como estas hoy?")
End If
%>
</body>
</html>
</body>
</html>
<html>
<body>
<%
if num="" then
%>
Bienvenido! Esta es la primera vez que visitas esta pagina web.
<%
else
%>
Tu has visitado esta pagina web <%response.write(num)%> veces
<%
end if
%>
</body>
</html>
<html>
<body>
</form>
</body>
</html>
<a href="31basic.asp?color=azul">Ejemplo</a><br>
<%
Response.Write(Request.QueryString)
%>
</body>
</html>
</body>
</html>
33. Muestra la hora y la fecha actuales. Según la hora del día muestra un mensajeggdg
<HTML>
<HEAD>
<TITLE>Servidor educado</TITLE>
</HEAD>
<BODY BGCOLOR="Wheat">
<FONT COLOR="Teal" SIZE="5"><B>
El servidor educado le informa que son las <% = Time %> del día <% = Date %>
</B></FONT>
<P><B>
<% If Hour(Now) < 8 Then %>
Estas no son horas de conectarse, estoy durmiendo!
<% ElseIf Hour(Now) < 15 Then %>
Estoy trabajando, no moleste.
<% Else %>
La tarde es para descansar, ver la tele, dormir, ...
<% End If
Response.Write("<BR><BR>")
Randomize
i = Int(Rnd * 4)
Select Case i
Case 0
Response.Write("Hace buen día, ¿ verdad ?")
Case 1
Response.Write("Me parece que hoy lloverá.")
Case 2
Response.Write("Esta tarde llueve seguro.")
Case 3
Response.Write("Que sol mas espléndido.")
End Select
%>
</B>
</BODY>
</HTML>
34. Permitiría detectar la lengua empleada por el navegador y visualizar un mensaje en dicha
lengua.
<% @ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>Detector de Lengua</TITLE>
</HEAD>
<BODY>
<%
'Antes de nada introducimos mensajes en forma de variables
espanol="Hola"
ingles="Hello"
frances="Bonjour"
<HTML>
<HEAD>
<TITLE>ASP Fácil - Código</TITLE>
sDestino=Trim(CStr(Request.Form("Para")))
sRemite=Trim(CStr(Request.Form("De")))
sBody=CStr(Request.Form("Cuerpo"))
Sub Formulario
%>
<TABLE BORDER="1" STYLE="background-color: lightgrey; color: black" WIDTH="400" ALIGN="center">
<FORM NAME="Email" ACTION="formemail.asp" METHOD="post">
<TR>
<TD COLSPAN="2">Introduce una dirección para enviarle un mail, y otra para que sea el remitente.
Si quieres, puedes añadir un comentario.</TD>
<TR>
<TD><B>Para:</B></TD>
<TD><INPUT TYPE="text" NAME="Para"></TD>
<TR>
<TD><B>De:</B></TD>
<TD><INPUT TYPE="text" NAME="De"></TD>
<TR>
<TD><B>Comentario:</B></TD>
<TD><TEXTAREA NAME="Cuerpo" WRAP="virtual" ROWS="3" COLS="15"></TEXTAREA></TD>
<TR>
<TD COLSPAN="2"><CENTER>
<INPUT TYPE="submit" VALUE="Enviar Email!">
<INPUT TYPE="reset">
</CENTER>
</TD>
</FORM>
</TABLE>
<%
Sub MandarMail
%>
nombrescript = Request.ServerVariables("URL")
textodeentrada = Request.Form("txtWordCount")
Function GetCharCount(strInput)
GetCharCount = Len(textodeentrada)
End Function ' GetCharCount
'**** Fin de funciones *************************************
%>
<p>Introduce el texto a contar:</p>
<br />
<input type="submit">
</form>
Primos.html
<html>
<head>
<title>Calculo de numeros primos</title>
</head>
<body>
<h2 align="center">Calculo de numeros primos</h2>
<center>
<table border="1" width="30%">
<tr>
<td width="100%"><p align="center">Introduce un numero</p>
<form method="POST" action="primos.asp">
<p><input type="text" name="numero" size="20"></p>
<center><p><input type="submit" value="Enviar" name="B1">
<input type="reset" value="Restablecer" name="B2"></p>
</center>
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
Primos.asp
<%option explicit%>
<html>
<% dim numero,contador,primo,result,doble
numero=request.form("numero")%>
<head>
<title>Calculo de numeros primos</title>
</head>
<body>
<hr>
<%if isnumeric(numero) then
doble = cdbl(numero)
numero=clng(numero)
if numero <> doble or numero > 30000000 then %>
<p align="center">
el numero no puede tener decimales ni ser mayor de 30000000
<%else
contador = 2
primo=true
do while contador < numero-1 and primo = true
if numero mod contador = 0 then
primo=false
else
contador=contador+1
end if
loop
if primo then
response.write "el numero "&numero &" es primo"
else
response.write "el numero "& numero& " no es primo, es divisible por " &contador
end if
end if
else%>
El campo de consulta debe ser numerico
<%end if%>
</p>
<hr align="center">
<td colspan="4">
<div align="center"><font size="2"
color="#666666"><img src="mante/images/<%=rs("imagen")%>" vspace="0"
hspace="5" border="0"><br>
<%
Response.Write "<b><font face=Verdana size=1><b><a href="
&rs.Fields("enlace") & " target=_blank>"&rs("libro")&"</a>
</b></font><br>"%>
</font></div><br>
</td>
<%
else
%>
<td colspan="4">
<%
Response.Write "<b><font face=Verdana size=1><b><a href=" &rs.Fields
("enlace") & " target=_blank>"&rs("libro")&"</a> </b></font><br>"%>
</td>
<%
end if
counter=counter+1
Rs.MoveNext
%>
<%If counter MOd 2=0 Then
Response.Write "</tr><tr>"
Loop
%>
</table>
<%Dim path
Dim iday
Dim imonth
'Cambia el path por el de las imagenes
path = "http://www.tuservidor.com/images/"
iday = day(now())
imonth = month(now())
Response.Write "<img border=""0"" src=""" & path & "m" & imonth & ".gif"">"
Response.Write "<img border=""0"" src=""" & path & "d" & iday & ".gif"">"
%>
global.asa
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
application("visitas")=0
application("fecha")=date()
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
application.lock
application("visitas")=application("visitas")+1
application.unlock
End Sub
Sub Session_OnEnd
End Sub
</SCRIPT>
mipagina.asp
<html>
........
Eres el visitante nº :<%=application("visitas")%> desde el dia <%=application("fecha")%>
.....
</html>
<%Option explicit%>
<HTML>
<HEAD><TITLE>Fecha</TITLE></HEAD>
<BODY>
<%
Dim ArrayFecha,ArrayHora,Anyo,Dia,Fecha,Hora,Mes,Minutos,Segundos
' Obtengo la fecha del servidor
Fecha=Date()
ArrayFecha = split(cdate(Fecha),"/")
function fechaASP()
' Esta funcion devuelve la fecha del servidor en formato largo
dim meses(12)
meses(0)=""
meses(1)="enero"
meses(2)="febrero"
meses(3)="marzo"
meses(4)="abril"
meses(5)="mayo"
meses(6)="junio"
meses(7)="julio"
meses(8)="agosto"
meses(9)="setiembre"
meses(10)="octubre"
meses(11)="noviembre"
meses(12)="diciembre"
fechaASP=Dia+" de "+meses(Mes)+" del "+Anyo
end function
%>
<br>
<script language="JavaScript">
<!--
function fechaJavaScript(Anyo,Mes,Dia) {
// Esta funcion devuelve la fecha local en formato largo
//NOTA: las siguientes 2 sentencias van juntas en la misma linea
var meses = new Array("","enero","febrero","marzo","abril","
mayo","junio","julio","agosto","setiembre","octubre","noviembre","diciembre")
<html>
<head><title>Listado de registros</title></head>
<center>
<table border="0" width="32%" bgcolor="#C0C0C0">
<tr>
<td width="100%">
<p align="center"><b><big>Listado de registros</big></b></td>
</tr>
</table>
<h3></center>
<br>
</h3>
<table BORDER="1" CELLSPACING="0" BORDERCOLOR="#000000"
CELLPADDING="2" width="100%">
<tr>
<td BGCOLOR="#C0C0C0"><b>Nombre de producto</b></td>
<td BGCOLOR="#C0C0C0"><b>Cantidad</b></td>
<td BGCOLOR="#C0C0C0" align="right"><b>Precio</b></td>
</tr>
<%
Do While NOT rs.EOF%>
<tr>
<td><%= rs("Producto")%></td>
<td><%= rs("Cantidad")%></td>
<td align="right"><%= FormatCurrency(rs("Precio"))%></td>
</tr>
<% rs.MoveNext
Loop
rs.Close
%>
</table>
</body></html>