본문 바로가기

IT

ASP 예제 코드 17개

ASP 예제 코드 17개

ASP 예제 코드 17개
ASP 예제 코드 17개

1. Displaying the current date and time:


<%
dim currentDate
currentDate = date()
response.write("Today's date is " & currentDate)
%>


=====================================

2. Redirecting to another page:


<%
response.redirect("https://www.example.com")
%>


=====================================

3. Creating a form with text inputs:


<form action="submit.asp" method="post">
<input type="text" name="name" placeholder="Enter your name">
<input type="text" name="email" placeholder="Enter your email">
<input type="submit" value="Submit">
</form>


=====================================

4. Retrieving data from a database:


<%
dim conn, rs, sql
set conn = server.createObject("ADODB.Connection")
conn.open "Driver={SQL Server};Server=localhost;Database=myDB;Uid=myUsername;Pwd=myPassword;"
sql = "SELECT * FROM Customers"
set rs = conn.execute(sql)
while not rs.EOF
response.write("Name: " & rs("Name") & "<br>")
response.write("Email: " & rs("Email") & "<br>")
rs.MoveNext
wend
rs.Close
set rs = nothing
conn.Close
set conn = nothing
%>


=====================================

5. Creating a session variable:


<%
session("user") = "John Doe"
response.write("Welcome, " & session("user"))
%>


=====================================

6. Writing a cookie:


<%
dim cookie
set cookie = response.cookies("visitcount")
cookie.expires = date() + 30
if cookie("count") = "" then
cookie("count") = 1
else
cookie("count") = cookie("count") + 1
end if
response.write("This is your " & cookie("count") & " visit to this site.")
%>


=====================================

7. Creating an if-else statement:


<%
dim score
score = 85
if score >= 60 then
response.write("Passed")
else
response.write("Failed")
end if
%>


=====================================

8. Creating a for loop:


<%
for i = 1 to 10
response.write(i & "<br>")
next
%>


=====================================

9. Creating a function:


<%
function addNumbers(a, b)
addNumbers = a + b
end function
response.write("The sum of 5 and 3 is " & addNumbers(5, 3))
%>


=====================================

10. Displaying a random image:


<%
dim images(3), randomNum
images(0) = "image1.jpg"
images(1) = "image2.jpg"
images(2) = "image3.jpg"
randomNum = Int(Rnd * 3)


=====================================

11. Creating an array:


<%
dim colors(3)
colors(0) = "red"
colors(1) = "green"
colors(2) = "blue"
response.write("The first color in the array is " & colors(0))
%>


=====================================

12. Creating a switch case statement:


<%
dim day
day = "Monday"
select case day
case "Monday"
response.write("Today is Monday.")
case "Tuesday"
response.write("Today is Tuesday.")
case "Wednesday"
response.write("Today is Wednesday.")
case else
response.write("Today is not Monday, Tuesday, or Wednesday.")
end select
%>


=====================================

13. Retrieving data from a query string:


<%
dim name
name = Request.QueryString("name")
response.write("Hello, " & name)
%>


=====================================

14. Creating a dynamic dropdown list:


<%
dim i, colors(3)
colors(0) = "red"
colors(1) = "green"
colors(2) = "blue"
%>
<select>
<% for i = 0 to UBound(colors) %>
<option value="<%=colors(i)%>"><%=colors(i)%></option>
<% next %>
</select>


=====================================

15. Uploading a file:


<form action="upload.asp" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<%
dim file
set file = Request.Files("file")
file.SaveAs Server.MapPath(".") & "\uploads\" & file.FileName
%>


=====================================

16. Creating a while loop:


<%
dim i
i = 1
while i <= 10
response.write(i & "<br>")
i = i + 1
wend
%>


=====================================

17. Sending an email:


<%
dim mail
set mail = server.createObject("CDO.Message")
mail.From = "sender@example.com"
mail.To = "recipient@example.com"
mail.Subject = "Test Email"
mail.TextBody = "This is a test email sent from ASP."
mail.Send