2004 tau yenny, si - binus m0194 web-based programming lanjut session 1

43
2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

Upload: roderick-todd

Post on 11-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

M0194

Web-based Programming LanjutSession 1

Page 2: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

2

Active Server Pages (ASP)

ASP Object Model Request Object Response Object Form and QueryString ServerVariable Collection

Page 3: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

3

ASP Object Model

Page 4: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

4

ASP Objects

Request ObjectMakes 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

Page 5: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

5

ASP Objects

Response Objectto 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

Page 6: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

6

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.

Page 7: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

7

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

Page 8: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

8

ASP Objects

Server ObjectProvides us with a series of methods and properties

that are useful in scripting with ASP

ASPError ObjectProvides a range of detailed information about the

last error that occurred in ASP.

Page 9: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

9

The ASP Request Object

The Request Object’s CollectionsCollection 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 read-only.

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.

Page 10: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

10

The ASP Request ObjectCollection Name Description

ServerVariables 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.

Property Description

TotalBytes Read-only. Returns the total number of bytes in the body of the request sent by the client.

The Request Object’s Property

Method Description

BinaryRead(count) 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 Request Object’s Method

Page 11: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

11

The ASP Response Object

The Response Object’s CollectionCollection Name Description

Cookies 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 Description

Buffer = True | False 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.

Page 12: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

12

The ASP Response ObjectProperty 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.

Page 13: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

13

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”.

Page 14: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

14

The ASP Response Object

The Response Object’s MethodsMethod 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.

Page 15: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

15

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.

Page 16: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

16

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 :

1. <HTML>2. <BODY>3. <FORM action = "show_request.asp" METHOD ="POST">4. FirstName : <INPUT TYPE = "TEXT" NAME =

"FirstName">5. LastName : <INPUT TYPE = "TEXT" NAME

="LastName">6. <INPUT TYPE = "SUBMIT" VALUE = "Send">7. </FORM>8. </BODY>9. </HTML>

Page 17: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

17

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.

1. <HTML>2. <BODY>3. <%4. strFirstName = Request.Form ("FirstName")5. strLastName = Request.Form ("LastName")6. Response.Write "Using text string key: <BR>"7. Response.Write strFirstName & "<BR> " & strLastName & "<BR>"

8. strFirstName2 = Request.Form(1)9. strLastName2 = Request.Form(2)10. Response.Write "<BR> Using index : <BR>"11. Response.Write strFirstName2 & "<BR> " & strLastName212. %>13. </BODY>14. </HTML>

show_request.asp

Page 18: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

18

Working with Form and QueryString Collection

Page 19: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

19

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. <HTML>2. <Body>3. <%4. strAllFormContent = Request.Form5. Response.Write strAllFormContent6. %>7. </body>8. </HTML>

Page 20: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

20

Working with Form and QueryString Collection

Page 21: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

21

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 indexFor intLoop = 1 to Request.Form.Count Response.Write Request.Form(intLoop) & “ <BR>”Next

A better method is use the For Each.. Next constructFor Each objItem In Request.Form Response.Write objItem & “ = “ & Request.Form(objItem) & “<BR>”Next

Page 22: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

22

Iterating Through an ASP Collection1. <HTML>2. <BODY>3. <%4. Response.Write "Using counter : <BR>"5. For intLoop = 1 to Request.Form.Count6. Response.Write Request.Form(intLoop) & " <BR>"7. Next

8. Response.Write "<BR>Using For Each :<BR>"9. For Each objItem In Request.Form10. Response.Write objItem & " = " & Request.Form(objItem) &

"<BR>"11. Next12. %>13. </BODY>14. </HTML>

show_request.asp

Page 23: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

23

Iterating Through an ASP Collection

Page 24: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

24

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:

default.html 1. <HTML>2. <BODY>3. <FORM ACTION = “show_request.asp” METHOD = “POST”>4. <INPUT TYPE=“TEXT” NAME=“OtherHobby”>5. <INPUT TYPE=“TEXT” NAME=“OtherHobby”>6. <INPUT TYPE=“TEXT” NAME=“OtherHobby”>7. <INPUT TYPE=“SUBMIT” VALUE=“Send”>8. </FORM>9. </BODY>10. </HTML>

show_request.asp 1. <HTML>2. <BODY>3. <%4. Response.Write Request.Form("OtherHobby")5. %>6. </BODY>7. </HTML>

Page 25: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

25

Multiple Value Collection Members

Page 26: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

26

Multiple Value Collection Members To be access individual values, we can use the rather

convoluted code :1. <HTML>2. <BODY>3. <%4. For Each objItem In Request.Form5. If Request.Form(objItem).Count > 1 Then 'If More than 1 value6. Response.Write objItem & " : <BR>"7. For intLoop = 1 to Request.Form(objItem).Count8. Response.Write "Subkey " & intLoop & " value = " _9. & Request.Form(objItem)(intLoop) &

"<BR>"10. Next11. Else12. Response.Write objItem & " = " & Request.Form(objItem) & "<BR>"13. End If14. Next15. %>16. </BODY>17. </HTML>

Page 27: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

27

Multiple Value Collection Members

Page 28: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

28

Multiple Value Collection Members1. <HTML>2. <BODY>3. <FORM ACTION = "show_request.asp" METHOD = "POST">4. <INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Next">5. <INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Previous">6. <INPUT TYPE="SUBMIT" NAME = "btnSubmit" VALUE="Cancel">7. </FORM>8. </BODY>9. </HTML>

1. <HTML>2. <BODY>3. <%4. Select Case Request.Form("btnSubmit")5. Case "Next"6. Response.Redirect "page_3.html"7. Case "Previous"8. Response.Redirect "page_1.html"9. Case "Cancel"10. Response.Redirect "default.html"11. End Select12. %>13. </BODY>14. </HTML>

Page 29: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

29

Multiple Value Collection Members

Page 30: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

30

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

Page 31: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

31

QueryString

1. <HTML>2. <BODY>3. <%4. strFirstName = Request.QueryString ("FirstName")5. strLastName = Request.QueryString ("LastName")6. strAll = Request.QueryString7. Response.Write "Using query string: <BR>"8. Response.Write strFirstName & "<BR> " & strLastName & "<BR>"9. Response.Write strAll10. %>11. </BODY>12. </HTML>

URL and query string

Page 32: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

32

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.

Page 33: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

33

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.

Page 34: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

34

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">

Page 35: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

35

Self-Referencing Pages

1. <HTML>2. <BODY>3. <FORM ACTION=" <% = Request.ServerVariables("SCRIPT_NAME") %>"

METHOD="POST">4. </FORM>5. <%6. strFullPath = Request.ServerVariables ("PATH_INFO")7. strPathOnly = Left(strFullPath, InStrRev(strFullPath, "/"))8. strNextPage = strPathOnly & "page_3.html"9. Response.Write "Full Path : " & strFullPath & "<BR> "10. Response.Write "Path Only : " & strPathOnly & "<BR> "11. Response.Write "Next Page : " & strNextPage & "<BR><BR> "12. %>13. <A HREF = " <% = strNextPage %> "> Next Page </A>14. </BODY>15. </HTML>

Page 36: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

36

Self-Referencing Pages

Page 37: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

37

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.

Page 38: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

38

Detecting the Browser Version1. <HTML>2. <BODY>3. <H1>Detecting the Browser Type</H1>4. <HR width=100%>5. <BR>6. <%7. strUA = Request.ServerVariables("HTTP_USER_AGENT")8. Response.Write "The User Agent string is <B>" & strUA & " </B><P>"

9. If InStr(StrUA, "MSIE") Then10. Response.Write "To upgrade your browser go to " _11. & "<A HREF=" & chr(34) & "http://www.microsoft.com/ie/" _12. & chr(34) & ">http://www.microsoft.com/ie/</A></P>"13. intVersion = CInt(Mid(strUA, InStr(StrUA,"MSIE") + 5, 1))

14. If intVersion >=4 then15. Response.Write "You can use Microsoft Dynamic HTML"16. End If17. End If18. %>19. </BODY>20. </HTML>

Page 39: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

39

Detecting the Browser Version

Page 40: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

40

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.

Page 41: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

411. <HTML>2. <BODY>3. <H1>Detecting the Browser Language</H1>4. <HR width=100%>5. <BR>6. <%7. strLocale = LCase(Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"))8. Response.Write "The Browser Language code is <B>" & strLocale & " - "

9. Select Case strLocale10. Case "en-gb" : Response.Write "English, United Kingdom"11. Case "en-us" : Response.Write "English, United States"12. Case "es-pe" : Response.Write "Spanish, Peru"13. Case "id" : Response.Write "Indonesian"14. case Else : Response.Write "Another Language"15. End Select

16. Response.Write "</B>"17. %>18. </BODY>19. </HTML>

Page 42: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

42

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.

Page 43: 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 1

2004 Tau Yenny, SI - Binus

431. <HTML>2. <BODY>3. <H1>Detecting the Browser</H1>4. <HR width=100%>5. <BR>6. <%7. Response.Write "PORT : " & Request.ServerVariables("SERVER_PORT") & "<BR>"8. If Request.ServerVariables("SERVER_PORT") = "80" Then9. Response.Write "Default PORT"10. Else11. Response.Write "Another PORT"12. End If13. %>14. </BODY>15. </HTML>