asp
Uses Request.___() and Response.___() for most operation
<% Response.Write("hello world <br />") %>
Output: hello world
GET
- Request.QueryString():
- mypage.asp?cat=5&name=dan -> Request.QueryString() => "cat=5&name=dan"
<html><body>
<form action="thispage.asp" method="get">
Your name: <input type="text" name="name" size="10" />
<input type="submit" value="Submit" />
</form>
<%
dim name
name=Request.QueryString("name")
If name<>"" Then
Response.Write("Name: " & name & "<br />")
End If
%>
</body></html>
POST
Request.Form(): ==> output: "cat=5&name=dan" if cat and name was posted in form
<html><body>
<form action="thispage.asp" method="post">
Your name: <input type="text" name="name" size="10" />
<input type="submit" value="Submit" />
</form>
<%
dim name
name=Request.Form("name")
If name<>"" Then
Response.Write("Name: " & name & "<br />")
End If
%>
</body></html>
Radio Button:
<html><body>
<%
dim cars
cars=Request.Form("cars")
%>
<form action="radiobutton.asp" method="post">
<p>Select Red or Green Color:</p>
<input type="radio" name="color"
<%if color="Red" then Response.Write("checked")%>
value="Red">Red</input>
<br />
<input type="radio" name="color"
<%if color="Green" then Response.Write("checked")%>
value="Green">Green</input>
<br />
<input type="submit" value="Submit" />
</form>
<%
if color <>"" then
Response.Write("<p>Color: " & color & "</p>")
end if
%>
</body></html>
Cookies
<html><body>
<%
; Counter example : dankim
dim visit_count
response.cookies("visit_count").Expires=date+5
; lasts 5 days
visit_count=request.cookies("visit_count")
if visit_count="" then
response.cookies("visit_count")=1
response.write("Counter is 0")
else
response.cookies("visit_count")=visit_count+1
response.write("Counter: " & visit_count)
end if
%>
</body></html>
Redirect
Response.Redirect("somewhere_to_go.asp")
<html><body>
<%
if Request.Form("url")<>"" then
Response.Redirect(Request.Form("url"))
end if
%>
<form action="thispage.asp" method="post">
<input type="radio" name="url"
value="contact.asp">
Go to contact page<br>
<input type="radio" name="url"
value="about.asp">
Go to about page<br>
<input type="submit" value="go">
</form>
</body></html>
Buffer
if Response.Buffer is set to True, it uses buffer system. Response is not sent until the buffer is full or until it is flushed
<%Response.Buffer=true
Response.Write(do_something_complicated_and_long())
; no output yet.
Response.Flush ; print output NOW!
%>
Response.Clear(): clears the buffer ie no text is sent!
<%Response.Clear %>
Request - Properties
ServerVariables('var')
- Request.ServerVariables("http_user_agent"): 'Mozilla...'
- Request.ServerVariables("remote_addr")): IP Address of viewer ie "15.1.2.44"
- Request.ServerVariables("request_method")): "GET" or "POST"
- Request.ServerVariables("server_name"): Domain name of server ie "www.bzdz.com"
Request.Totalbytes: Size of request
Response - Misc Functions
Response.End(): Ends response immediately, ignoring anything below it.
Response - Properties
Response.ContentType="text/html"
Response.Charset="ISO8859-1"
Response.Expires
Session
Session.SessionID: session id # ie "1345123" Session.Timeout: timeout in minutes. Session.Timeout=30 ; 30 minute
Dictionary
dict.Count: dict size
dict.Keys() : array of keys.
dict.Items(): array of items
dict.Item("key"): item given the key name
dict.Key("key")= "new_key_name" : replace key name
<%
dim dict
set dict=Server.CreateObject("Scripting.Dictionary")
dict.Add "m", "Male"
if dict.Exists("f")= false then
dict.Add "f", "Female"
end if
' iterate
for i = 0 To dict.Count -1
Response.Write( dict.Keys(i) & "="& dict.Items(i) & "<br>")
next
set dict=nothing 'erase dict
%>
Other services
- set adrotator=Server.CreateObject("MSWC.AdRotator")
- Changes ad each time. See doc for more info
- set nextlink=server.createobject("MSWC.Nextlink")
- builds table of contents
Read a text File
OpenTextFile(Server.MapPath(path))
ReadAll(): Read entire text file
ReadLine(): Read current one line
Read(n): Read n characters
- ::
<% Set FSO = Server.CreateObject("Scripting.FileSystemObject") Set file = FSO.OpenTextFile(Server.MapPath("mytexts") & "readme.txt",1) While not file.AtEndOfStream
Response.Write (file.ReadLine & "<br />")
Wend %>
File-based hit counter:
<%
'writing is disabled due to write-access security (ie file.Write)
Set FSO=Server.CreateObject("Scripting.FileSystemObject")
Set file=FSO.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
counter=file.ReadLine
file.Close
counter=counter+1
Set file=Nothing 'free mem
Set FSO=Nothing
%>
<html><body>
Counter:<%=counter%>.
</body></html>
Functions
- File exist test::
- If (FSO.FileExists("c:webmyfile.txt"))=true Then ...
- Folder Exists
- FolderExists("c:web") = true
GetParentFolderName("d:fullpathfile.txt")
GetExtensionName("something.txt") : "txt"
GetBaseName("c:webindex.html") : "index" GetBaseName("c:webtmp") : "tmp"
Date
- Day of the week::
- <% response.Write(WeekdayName(weekday(date))) response.Write("<br>") response.Write(WeekdayName(weekday(date), true)) %>
- Output::
- Monday mo
Month and Date:
<%response.write(MonthName(month(date)))%>
<% if IsDate("5/5/2006") then
response.Write("OK")
end if
response.write("Invalid Date Format")
%>
String
- ucase, lcase::
- <% response.write(ucase("lowly")) response.write(lcase("MAN")) %>
trim, ltrim, rtrim
strReverse
