Web-based Programming Lanjut Pertemuan 1 Matakuliah : M0492 / Web-based Programming Lanjut Tahun

advertisement
Matakuliah : M0492 / Web-based Programming Lanjut
Tahun
: 2007
Web-based Programming Lanjut
Pertemuan 1
Active Server Pages (ASP)
•
•
•
•
•
Bina Nusantara
ASP Object Model
Request Object
Response Object
Form and QueryString
ServerVariable Collection
ASP Object Model
Bina Nusantara
ASP Objects
• Request Object
Makes available to our script all the information that the client
provides when requesting a page or submitting a form
Include :
• the HTTP variables that identify the browser and the user
• the cookies that they have stored on their browser for this
domain
• any values appended to the URL, either as a query string or in
HTML controls in a <FORM> section of the page
Bina Nusantara
ASP Objects
• Response Object
to access the response that we are creating to send back to the
client.
It makes available :
- to our script the HTTP variables that identify our server and its
capabilities
- information about the content we’re sending to the browser
- any new cookies that will be stored on their browser for this
domain
Provides a series of method that we can use to create output
Bina Nusantara
ASP Objects
• Application Object
Created when the ASP DLL is loaded in response to the first request
for an ASP page.
Provides a repository for storing variables and object references
that are available to all the pages, which any visitor can open.
Bina Nusantara
ASP Objects
• Session Object
Created for each visitor when they first request an ASP page from
the Web site or Web application, and it remains available until the
default timeout period expires.
Provides a repository for storing variables and object references
that are available only to the pages that visitor opens during the
lifetime of the session
Bina Nusantara
ASP Objects
• Server Object
Provides us with a series of methods and properties that are useful
in scripting with ASP
• ASPError Object
Provides a range of detailed information about the last error that
occurred in ASP.
Bina Nusantara
The ASP Request Object
The Request Object’s Collections
Collection Name
Description
ClientCertificate
A collection of the values of all the fields or entries in the client certificate that the user presented to
our server when accessing a page or resource. Each member is read-only.
Cookies
A collection of the values of all the cookies sent from the user’s system along with their request. Only
cookies valid for the domain containing the resource are sent to the server. Each member is readonly.
Form
A collection of the values of all the HTML control elements in the <FORM> section that was submitted
as the request, where the value of the METHOD attribute is POST. Each member is read-only.
QueryString
A collection of all the name/value pairs appended to the URL in the user’s request, or the values of all
the HTML control elements in the <FORM> section that was submitted as the request where the value
of the METHOD attribute is GET or the attribute is omitted. Each member is read-only.
Bina Nusantara
The ASP Request Object
Collection Name
ServerVariables
Description
A collection of all the HTTP header values sent from the client with their request, plus the values of
several environment variables for the Web server. Each member is read-only.
The Request Object’s Property
Property
TotalBytes
Description
Read-only. Returns the total number of bytes in the body of the request sent by the client.
The Request Object’s Method
Method
BinaryRead(count)
Bina Nusantara
Description
Retrieve count bytes of data from the client’s request when the data is sent to the server as part of
a POST request. It returns a Variant array (or SafeArray). Cannot be used successfully if the ASP
code has already referenced the Request.Form collection. Likewise, the Request.Form collection
cannot be successfully accessed if you have used the BinaryRead method.
The ASP Response Object
The Response Object’s Collection
Collection Name
Cookies
Description
A collection containing the values of all the cookies that will be sent back to the client in the
current response. The collection is write only.
The Response Object’s Properties
Property
Buffer = True | False
Bina Nusantara
Description
Read/write. Boolean. Specifies if the output created by an ASP page will be held in the IIS
buffer until all of the server scripts in the current page have been processed or until the Flush
or End method is called. It must be set before any output is sent to IIS, including HTTP header
information, so it should be the first line of the .asp file after the <%LANGUAGE=..%>
statement. Buffering is on (True) by default in ASP 3.0, whereas it was off (False) by default in
earlier versions.
The ASP Response Object
Property
Description
CacheControl “setting”
Read/write. String. Set this property to “Public” to allow proxy servers to cache the page, or “Private”
to prevent proxy caching taking place.
Charset = “value”
Read/write. String. Appends the name of the character set (for example, ISO-LATIN-7) to the HTTP
Content Type header created by the server for each response.
ContentType = “MIME-type”
Read/write. String. Specifies the HTTP content type for the response, as a standard MIME-type (such as
“text/xml” or image/gif”). If omitted, the MIME-type “text/html” is used. The content type tells the
browser what type of content to expect.
Expires minutes
Read/write. Number. Specifies the length of time in minutes that a page is valid for. If the user returns
to the same page before it expires, the cached version is displayed. After that period, it expires, and
will not be held in a private (user) or public (proxy) cache.
ExpiresAbsolute
#date[time]#
Read/write. Date/Time. Specifies the absolute date and time when a page will expire and no longer be
valid. If the user returns to the same page before it expires, the cached version displayed. After that
time, it expires, and should not be held in a private (user) or public (proxy) cache.
Bina Nusantara
The ASP Response Object
Property
Description
IsClientConnected
Read-only. Boolean. Return an indication of whether the client is still connected to and loading the
page from the server. Can be used to end processing (with the Response.End method) if a client
moves to another page before the current one has finished executing.
PICS (“PICS-label-string”)
Write only. String. Creates a PICS headers define the content of the page in terms of violence, sex,
bad language, etc.
Status = ”code message”
Read/Write. String. Specifies the status value and message that will be sent to the client in the
HTTP headers of the response to indicate an error or successful processing of the page. Example
are “200 OK” and “404 Not Found”.
Bina Nusantara
The ASP Response Object
The Response Object’s Methods
Method
Description
AddHeader (“name”, “content”)
Creates a custom HTTP header using the name and content values and adds it to the response. Will
not replace an existing header of the same name. Once a header has been added, it cannot be
removed. Must be used before any page content (i.e. text and HTML) is sent to the client.
AppendToLog (“string”)
Adds a string to the end of the Web server log entry for this request when W3C Extended Log File
Format is in use. Requires at least the URI Stem value to be selected in the Extended Properties
page for the site containing the page.
BinaryWrite (SafeArray)
Writes the content of a Variant-type SafeArray to the current HTTP output stream without any
character conversion. Useful for writing non-string information, such as binary data required by a
custom application or the bytes to make up an image file.
Clear ( )
Erases any existing buffered page content from the IIS response buffer when Response.Buffer is
True. Does not erase HTTP response headers. Can be used to abort a partly completed page.
Bina Nusantara
The ASP Response Object
Method
Description
End ( )
Stops ASP from processing the page script and returns the currently created content, then aborts
any further processing of this page.
Flush ( )
Sends all currently buffered page content in the IIS buffer to the client when Response.Buffer is
True. Can be used to send parts of a long page to the client individually.
Redirect (“url”)
Instructs the browser to load the page in the string url parameter by sending a “302 Object
Moved” HTTP header in the response.
Write (“string”)
Writes the specified string to the current HTTP response stream and IIS buffer, so that it becomes
part of the returned page.
Bina Nusantara
Working with Form and QueryString Collection
• ASP collection are an array of values, can be accessed using a text
string key (not case sensitive) or an integer index.
For example, in default.html :
Bina Nusantara
1.
2.
3.
4.
5.
6.
7.
8.
<HTML>
<BODY>
<FORM action = "show_request.asp" METHOD ="POST">
FirstName : <INPUT TYPE = "TEXT" NAME = "FirstName">
LastName : <INPUT TYPE = "TEXT" NAME ="LastName">
<INPUT TYPE = "SUBMIT" VALUE = "Send">
</FORM>
</BODY>
9.
</HTML>
Working with Form and QueryString Collection
• We can access the values by accessing the ASP Form Collection
• We can also use the integer index of the control on the form. The range of indexes
starts at 1 for the 1st control defined in the HTML. This technique is not
recommended.
show_request.asp
1.
2.
3.
4.
5.
6.
7.
Bina Nusantara
8.
9.
10.
11.
12.
13.
14.
<HTML>
<BODY>
<%
strFirstName = Request.Form ("FirstName")
strLastName = Request.Form ("LastName")
Response.Write "Using text string key: <BR>"
Response.Write strFirstName & "<BR> " & strLastName & "<BR>"
strFirstName2 = Request.Form(1)
strLastName2 = Request.Form(2)
Response.Write "<BR> Using index : <BR>"
Response.Write strFirstName2 & "<BR> " & strLastName2
%>
</BODY>
</HTML>
Working with Form and QueryString Collection
Bina Nusantara
Working with Form and QueryString Collection
•
It’s also possible to collect the entire set of values from the form into a single string variable by just
referencing the collection, and without providing a key or index.
strAllFormContent = Request.Form
If our text boxes contain the values Yos and Stefan Hideki, the Request.Form statement will return the string:
FirstName=Yos&LastName=Stefan+Hideki
The values are provided in name/value pairs. Each name/value pair is separated from the others with an
ampersand (&) character.
1.
2.
3.
4.
5.
6.
7.
8.
Bina Nusantara
<HTML>
<Body>
<%
strAllFormContent = Request.Form
Response.Write strAllFormContent
%>
</body>
</HTML>
Working with Form and QueryString Collection
Bina Nusantara
Iterating Through an ASP Collection
• There are 2 ways to iterate through all the members of an ASP collection.
• We can using an For and integer index
For intLoop = 1 to Request.Form.Count
Response.Write Request.Form(intLoop) & “ <BR>”
Next
• A better method is use the For Each.. Next construct
For Each objItem In Request.Form
Response.Write objItem & “ = “ & Request.Form(objItem) & “<BR>”
Next
Bina Nusantara
Iterating Through an ASP Collection
show_request.asp
1.
2.
3.
4.
5.
6.
7.
<HTML>
<BODY>
<%
Response.Write "Using counter : <BR>"
For intLoop = 1 to Request.Form.Count
Response.Write Request.Form(intLoop) & " <BR>"
Next
8.
Response.Write "<BR>Using For Each :<BR>"
9.
For Each objItem In Request.Form
10.
Response.Write objItem & " = " & Request.Form(objItem) & "<BR>"
11.
Next
12. %>
13. </BODY>
14. </HTML>
Bina Nusantara
Iterating Through an ASP Collection
Bina Nusantara
Multiple Value Collection Members
•
In some cases, an individual member of an ASP collection may be made up of more than one value. This occurs when several controls have the same NAME attribute
defined in the HTML, for example:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
default.html
<HTML>
<BODY>
<FORM ACTION = “show_request.asp” METHOD = “POST”>
<INPUT TYPE=“TEXT” NAME=“OtherHobby”>
<INPUT TYPE=“TEXT” NAME=“OtherHobby”>
<INPUT TYPE=“TEXT” NAME=“OtherHobby”>
<INPUT TYPE=“SUBMIT” VALUE=“Send”>
</FORM>
</BODY>
</HTML>
1.
2.
3.
4.
5.
6.
7.
show_request.asp
<HTML>
<BODY>
<%
Response.Write Request.Form("OtherHobby")
%>
</BODY>
</HTML>
Bina Nusantara
Multiple Value Collection Members
Bina Nusantara
Multiple Value Collection Members
• To be access individual values, we can use the rather convoluted code :
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Bina Nusantara
<HTML>
<BODY>
<%
For Each objItem In Request.Form
If Request.Form(objItem).Count > 1 Then 'If More than 1 value
Response.Write objItem & " : <BR>"
For intLoop = 1 to Request.Form(objItem).Count
Response.Write "Subkey " & intLoop & " value = " _
& Request.Form(objItem)(intLoop) & "<BR>"
Next
Else
Response.Write objItem & " = " & Request.Form(objItem) & "<BR>"
End If
Next
%>
</BODY>
</HTML>
Multiple Value Collection Members
Bina Nusantara
Multiple Value Collection Members
1.
2.
3.
4.
5.
6.
7.
8.
9.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Bina Nusantara
<HTML>
<BODY>
<FORM ACTION = "show_request.asp" METHOD = "POST">
<INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Next">
<INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Previous">
<INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Cancel">
</FORM>
</BODY>
</HTML>
<HTML>
<BODY>
<%
Select Case Request.Form("btnSubmit")
Case "Next"
Response.Redirect "page_3.html"
Case "Previous"
Response.Redirect "page_1.html"
Case "Cancel"
Response.Redirect "default.html"
End Select
%>
</BODY>
</HTML>
Multiple Value Collection Members
Bina Nusantara
The Difference Between Forms and QueryString
•
Append 1 or more name/value pair to the URL of the page we’re requesting, they become the
query string for the request, and are exposed to our ASP page in the QueryString collection.
•
Therefore, the only way to send values to ASP from any of these actions is through the
QueryString collection, by appending them to the URL
•
The URL/query String combination cannot contain any spaces or other illegal characters.
Illegal characters are those that are used to delimit parts of the URL and query string, for
example ‘/’, ‘:’, ‘?’, and ‘&’.
Spaces must be convert to ‘+’ format.
ASP Server object provides URLEncode method for handling this conversion.
•
The values appear in the Request.QueryString, and are accessed, in the same way as the
FORM collection examples.
The URL and query string combination :
http://localhost/ok/show_request.asp?FirstName=Yos&LastName=Stefan+Hideki
Bina Nusantara
QueryString
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
<HTML>
<BODY>
<%
strFirstName = Request.QueryString ("FirstName")
strLastName = Request.QueryString ("LastName")
strAll = Request.QueryString
Response.Write "Using query string: <BR>"
Response.Write strFirstName & "<BR> " & strLastName & "<BR>"
Response.Write strAll
%>
</BODY>
</HTML>
URL and query
string
Bina Nusantara
The GET and POST Methods of a FORM
• There are 2 common method for requesting a page or resource from a Web server
over HTTP.
– GET
To get the resource directly. Get is the default method.
– POST
To post values to a resource.
Bina Nusantara
The GET and POST Methods of a FORM
• Use “GET” or omit the attribute, the browser bundles up the values in all the
controls on the form into a query string, and appends it to the URL of the page
being requested. The request is exposed through the ASP Request.QueryString
collection.
• Use “POST”, the browser wraps the values up within the HTTP headers it sends to
the server, and they are exposed to ASP via the Request.Form collection.
Bina Nusantara
ServerVariables Collection
• Contains a combination of values that represent the HTTP headers sent from the
client
• Self-Referencing Pages
Detail of our Web server and path information of the current page.
<FORM ACTION=" <% = Request.ServerVariables("PATH_INFO") %>"
METHOD="POST">
The same effect is obtained with the HTTP “SCRIPT_NAME” value :
<FORM ACTION=" <% = Request.ServerVariables("SCRIPT_NAME") %>"
METHOD="POST">
Bina Nusantara
Self-Referencing Pages
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Bina Nusantara
<HTML>
<BODY>
<FORM ACTION=" <% = Request.ServerVariables("SCRIPT_NAME") %>"
METHOD="POST">
</FORM>
<%
strFullPath = Request.ServerVariables ("PATH_INFO")
strPathOnly = Left(strFullPath, InStrRev(strFullPath, "/"))
strNextPage = strPathOnly & "page_3.html"
Response.Write "Full Path : " & strFullPath & "<BR> "
Response.Write "Path Only : " & strPathOnly & "<BR> "
Response.Write "Next Page : " & strNextPage & "<BR><BR> "
%>
<A HREF = " <% = strNextPage %> "> Next Page </A>
</BODY>
</HTML>
Self-Referencing Pages
Bina Nusantara
ServerVariables Collection
• Detecting the Browser Version
Another useful value is the user agent string for our visitor’s
browser.
In the Detecting Browser Type page (browsertype.asp), we are
using the “HTTP_USER_AGENT” value from the ServerVariables
collection to retrieve the user agent string, and some script to
parse this and look for a manufacturer name and browser version.
Bina Nusantara
Detecting the Browser Version
1.
2.
3.
4.
5.
6.
7.
8.
<HTML>
<BODY>
<H1>Detecting the Browser Type</H1>
<HR width=100%>
<BR>
<%
strUA = Request.ServerVariables("HTTP_USER_AGENT")
Response.Write "The User Agent string is <B>" & strUA & " </B><P>"
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
Bina Nusantara
If InStr(StrUA, "MSIE") Then
Response.Write "To upgrade your browser go to " _
& "<A HREF=" & chr(34) & "http://www.microsoft.com/ie/" _
& chr(34) & ">http://www.microsoft.com/ie/</A></P>"
intVersion = CInt(Mid(strUA, InStr(StrUA,"MSIE") + 5, 1))
If intVersion >=4 then
Response.Write "You can use Microsoft Dynamic HTML"
End If
End If
%>
</BODY>
</HTML>
Detecting the Browser Version
Bina Nusantara
ServerVariables Collection
• Detecting the Browser Language
“HTTP_ACCEPT_LANGUAGE” contains a language code, which was selected when
the browser was installed or is hard-coded into the user’s locale-specific version.
Example of language code are en-us (English, United States), de-at (German,
Austrian) and es-pe (Spanish, Peru).
A language code can also be generic and omit the dialect identifier.
We can detect the language code and load an appropriate locale-specific,
language-specific or geographically-specific version of our pages automatically.
Bina Nusantara
1.
2.
3.
4.
5.
6.
7.
8.
<HTML>
<BODY>
<H1>Detecting the Browser Language</H1>
<HR width=100%>
<BR>
<%
strLocale = LCase(Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"))
Response.Write "The Browser Language code is <B>" & strLocale & " - "
9.
10.
11.
12.
13.
14.
15.
Select Case strLocale
Case "en-gb" : Response.Write "English, United Kingdom"
Case "en-us" : Response.Write "English, United States"
Case "es-pe" : Response.Write "Spanish, Peru"
Case "id"
: Response.Write "Indonesian"
case Else
: Response.Write "Another Language"
End Select
16.
17.
18.
19.
Response.Write "</B>"
Bina Nusantara
%>
</BODY>
</HTML>
ServerVariables Collection
• Other Useful ServerVariables Values
– Check if the visitor accessed our site through the default port 80
or a different one.
• We can query the user name if we force our users to log on and be
authenticated by our server.
– “AUTH_USER”
Return the authenticated user string.
– “SERVER_NAME”
Return the server name string.
Bina Nusantara
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Bina Nusantara
<HTML>
<BODY>
<H1>Detecting the Browser</H1>
<HR width=100%>
<BR>
<%
Response.Write "PORT : " & Request.ServerVariables("SERVER_PORT") & "<BR>"
If Request.ServerVariables("SERVER_PORT") = "80" Then
Response.Write "Default PORT"
Else
Response.Write "Another PORT"
End If
%>
</BODY>
</HTML>
Download