1 / 59

Active Server Page - ASP in JavaScript

Active Server Page - ASP in JavaScript. 王金龍、陳彥錚 銘傳大學 資管系. Content. Introduction Object Models Request Object Response Object Server Object Application and Session Objects Installable Components for ASP. Introduction.

dougal
Download Presentation

Active Server Page - ASP in JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Active Server Page - ASPin JavaScript 王金龍、陳彥錚 銘傳大學 資管系

  2. Content • Introduction • Object Models • Request Object • Response Object • Server Object • Application and Session Objects • Installable Components for ASP

  3. Introduction • Microsoft’s newest server-based technology for building dynamic interactive web pages • No compiler • Text editor • Browser independent • Object-oriented • Compatible to any ActiveX scripting • Transparent to users

  4. ASP Usage <%@language=JScript%> <html> <head> <title>Active Server Scripting</title> </head> <body> <h3>Active Server Scripting </h3> <% Response.Write("This is so cool!!!”)%> </body> </html>

  5. Complete ASP Program <%@Language=JScript %> <HTML><HEAD> …</HEAD><BODY> <% … ASP script which runs on the server … %> <SCRIPT LANGUAGE=“JavaScript”> Script which run in the browser </SCRIPT> <SCRIPT LANGUAGE=“JScript” RUNAT=Server> Script which run in the server </SCRIPT> <!-- #include … --> <table> … </table> <% … %> </BODY></HTML>

  6. Server-side includes • <!-- #include file=“include.txt” --> • Include text files in pages • Virtual file addresses • <!-- #include virtual=“/u99/file.txt” --> • Physical file addresses • <!-- #include file=“c:\inetpub\user99\file.txt” --> • <!-- #include file=“file.txt” -->

  7. <!-- menu.inc --><A HREF="top.htm">Top</A><BR><A HREF="next.htm">Next</A><BR><A HREF="previous.htm">Previous</A><BR><P> … <body> <h3>Form Use </h3> <!--#INCLUDE FILE="menu.inc"--> <% Response.Write(Request.Form("text1")); %> </body>

  8. Basic Statements • <% Response.Write(“string”) %> • Output a string ( more readable ) • <% = “string” %> or <%=VarExpression%> • Insert a string • <%=Request.Form(“userName”)%> • <% Response.Redirect(“URL”) %> • Redirect to the URL • <% // Some Comments %>

  9. An ASP Example <%@language=JScript%> <% num=Request.Form("numOfHr"); msg="Welcome to My ASP Example!"; %> <html> <head><title>An ASP Example</title></head> <body> You are <b><%=Request.Form("userName")%></b>!<br> <%=msg%> <hr> <% for ( i=1; i<=num; i++) { %> <font color=blue>Iteration <i><%=i%></i></font> <% Response.Write("<hr width="+i*50+" align=left>"); } %> </body> </html>

  10. aspExample1.html <html> <body> <form method="post" action="aspExample1.asp"> Please input a number: <input type=text name="numOfHr" size=4><br> Your Name: <input type=text name="userName"><br> <input type=submit> </form> </body> </html>

  11. Source File in Web Client <html> <head><title>An ASP Example</title></head> <body> You are <b>Yen-Cheng Chen</b>!<br> Welcome to My ASP Example! <hr> <font color=blue>Iteration <i>1</i></font> <hr width=50 align=left> <font color=blue>Iteration <i>2</i></font> <hr width=100 align=left> <font color=blue>Iteration <i>3</i></font> <hr width=150 align=left> <font color=blue>Iteration <i>4</i></font> <hr width=200 align=left> <font color=blue>Iteration <i>5</i></font> <hr width=250 align=left> </body> </html>

  12. Built-In ASP Objects • Request Object • To retrieve the values that the client browser passed to the server during an HTTP request. • Response Object • To send output to the client. • Server • Provide utility functions on the server.

  13. Built-In ASP Objects (cont.) • Application • To share information among all users of a given application. (Like global variables) • Session • To store information needed for a particular user-session. (Like local variables) • ObjectContext • To commit or abort a transaction, managed by Microsoft Transaction Server (MTS).

  14. Client RequestObject Collection: Form QueryString ServerVariables Cookie ClientCertificate ResponseObject Collection: Cookie (Properties) (methods) Server ApplicationObject (properties) (methods) ServerObject (method) SessionObject (properties) (methods)

  15. Request Object • Provide all the information about the user’s request to applications • Collection • A data store that can store values by linking each one to a unique key

  16. Collections in Request Object • Five collections • QueryString: HTTP query string (GET) • Form: Form elements (POST) • ServerVariables: HTTP and environment variables • Cookie: Cookie sent from the browser • ClientCertificate: Certificate values (SSL: https) • Usage variable = Request.collectionName(“key”)

  17. Properties and Methods • Properties • TotalBytes: Read-only. Specifies the total number of bytes the client is sending in the body of the request. • Methods • BinaryRead(count): Retrieves data sent to the server from the client as part of a POST request.

  18. QueryString Collection: Get • The names and values of form are put into a query string • The amount of data can be sent is limited to around 1000 characters • Usage • variable = Request.QueryString(“name”)

  19. queryTest.asp queryTest.asp?name=Mickey+Mouse&age=50 <%@language=JScript%> <html><head><title>Query String Test ASP</title></head><body> You are <font color=red><%=Request.QueryString("name")%></font><br> You are <font color=red><%=Request.QueryString("age")%></font> years old. </body></html>

  20. <HTML><HEAD><TITLE></TITLE></HEAD><BODY><H3>Passing Name=Value Pairs with a Query String</H3><A NAME="product" HREF="list7_5.asp?name=dina&pub=sams">Sams Publishing </A></BODY></HTML> <html> <head> <title></title> </head> <body> <h3>Getting Name=Value Pairs with a Query String</h3> <p>name = <%= Request.QueryString("name") %></p> <p>pub = <% = Request.QueryString("pub") %> </p> </body> </html>

  21. Form Collection: Post • The name and values of the form are encoded into the request header • Usage • variable = Request.Form(“name”)

  22. formTest.html <html><head> <title>the form</title></head><body> <form name="form1" method=post action="formTest.asp"> text1: <input type=text name="text1" size=20><br> radio1: <input type=radio name="radio1" value="yes">yes <input type=radio name="radio1" value="no">no<br> select1: <select name="select1"> <option>option 1<option>option 2<option>option 3 </select><br> select2: (multiple) <select name="select2" multiple> <option>option 1M<option>option 2M<option>option 3M </select><br> textarea1: <textarea name="textArea1" cols=10 rows=5> </textarea><br> <input type=hidden name="hidden1" value="a hidden value"> <input type=submit value="o.k.!"> <input type=reset value="cancel"> </form> </body></html>

  23. formTest.asp <%@language=JScript%> <html><head><title>Form Test ASP</title></head><body> <table border=2> <tr><td>text1</td><td><%=Request.Form("text1")%></td></tr> <tr><td>radio1</td><td><%=Request.Form("radio1")%></td></tr> <tr><td>select1</td><td><%=Request.Form("select1")%></td></tr> <%num=Request.Form("select2").Count; for (i=1;i<=num;i++) { %> <tr><td>select2</td> <td><%=Request.Form("select2")(i)%></td></tr> <% } %> <tr><td>hidden1</td> <td><%=Request.Form("hidden1")%></td></tr> <tr><td>textArea1</td> <td><%=Request.Form("textArea1")%></td></tr> </table> </body></html>

  24. ServerVariables Collections • Provide HTTP header that is sent by a client browser • To Retrieves the values of predetermined environment variables. • Usage • variable = Request.ServerVariables(“HeaderType”)

  25. AUTH_TYPE = <%= Request.ServerVariables("AUTH_TYPE") %><br> CONTENT_LENGTH = <% = Request.ServerVariables("CONTENT_LENGTH") %><br> CONTENT_TYPE = <% = Request.ServerVariables("CONTENT_TYPE") %><br> GATEWAY_INTERFACE = <% = Request.ServerVariables(“GATEWAY_INTERFACE”) %><br> LOGON_USER = <% = Request.ServerVariables("LOGON_USER") %><br> PATH_INFO = <% = Request.ServerVariables("PATH_INFO") %><br> PATH_TRANSLATED = <% = Request.ServerVariables("PATH_TRANSLATED") %><br> QUERY_STRING = <% = Request.ServerVariables("QUERY_STRING") %><br> REMOTE_ADDR = <% = Request.ServerVariables("REMOTE_ADDR") %><br> REMOTE_HOST = <% = Request.ServerVariables("REMOTE_HOST") %><br> REMOTE_METHOD = <% = Request.ServerVariables("REMOTE_METHOD") %><br> SCRIPT_MAP = <% = Request.ServerVariables("SCRIPT_MAP") %><br> SCRIPT_NAME = <% = Request.ServerVariables("SCRIPT_NAME") %><br> SERVER_NAME = <% = Request.ServerVariables("SERVER_NAME") %><br> SERVER_PORT = <% = Request.ServerVariables("SERVER_PORT") %><br> SERVER_PORT_SECURE = <%=Request.ServerVariables("SERVER_PORT_SECURE") %><br> SERVER_PROTOCOL = <% = Request.ServerVariables("SERVER_PROTOCOL") %><br> SERVER_SOFTWARE = <% = Request.ServerVariables("SERVER_SOFTWARE") %><br> URL = <% = Request.ServerVariables("URL") %><br>

  26. ALL_HTTP All HTTP headers sent by the client. ALL_RAW All headers in the raw-form. APPL_MD_PATH The metabase path for the (WAM) Application for the ISAPI DLL. APPL_PHYSICAL_PATH Retrieves the physical path corresponding to the metabase path. AUTH_PASSWORD The value entered in the client's authentication dialog. AUTH_TYPE The authentication method that the server uses to validate users. AUTH_USER Raw authenticated user name. CERT_COOKIE Unique ID for client certificate, Returned as a string. CERT_FLAGS bit0 is set to 1 if the client certificate is present. bit1 is set to 1 if the client Certifying Authority is invalid. CERT_ISSUER Issuer field of the client certificate. CERT_KEYSIZE Number of bits in Secure Sockets Layer connection key size. CERT_SECRETKEYSIZE Number of bits in server certificate private key. CERT_SERIALNUMBER Serial number field of the client certificate. CERT_SERVER_ISSUER Issuer field of the server certificate. CERT_SERVER_SUBJECT Subject field of the server certificate. CERT_SUBJECT Subject field of the client certificate. CONTENT_LENGTH The length of the content as given by the client. CONTENT_TYPE The data type of the content. GATEWAY_INTERFACE The revision of the CGI specification used by the server. HTTP_<HeaderName> The value stored in the header HeaderName. HTTPS ON : if the request came in through secure channel (SSL). OFF : Otherwise.

  27. HTTPS_KEYSIZE Number of bits in Secure Sockets Layer connection key size. HTTPS_SECRETKEYSIZE Number of bits in server certificate private key. HTTPS_SERVER_ISSUER Issuer field of the server certificate. HTTPS_SERVER_SUBJECT Subject field of the server certificate. INSTANCE_ID The ID for the IIS instance in textual format. INSTANCE_META_PATH Metabase path for the IIS instance that responds to the request. LOCAL_ADDR Returns the Server Address on which the request came in. LOGON_USER The Windows NTR account that the user is logged into. PATH_INFO Extra path information as given by the client. PATH_TRANSLATED A translated version of PATH_INFO (virtual-to-physical) QUERY_STRING Query information after ? in the HTTP request. REMOTE_ADDR The IP address of the remote host making the request. REMOTE_HOST The name of the host making the request. REMOTE_USER Unmapped user-name string sent in by the User. REQUEST_METHOD The method used to make the request. SCRIPT_NAME A virtual path to the script being executed. SERVER_NAME The server's host name, DNS alias, or IP address. SERVER_PORT The port number to which the request was sent. SERVER_PORT_SECURE 1 : If the request is on the secure port. 0 : Otherwise. SERVER_PROTOCOL The name and revision of the request information protocol. SERVER_SOFTWARE The name and version of the server software. URL Gives the base portion of the URL.

  28. Cookie Collection • The cookie is a text file stored on the client • Use Request object to access the cookie • Read only • To change cookie: Use Response object • Usage • variable = Request.Cookies(“cookieVariable”)

  29. Response Object • To send output to the client • Collection: Cookie • Properties • Buffer • CacheControl • Charset • ContentType • Expires • ExpiresAbsolute • isClientConnected • Pics • Status • Method • AddHeader(name, value) • AppendToLog(string) • BinaryWrite(data) • Clear() • End() • Flush() • Redirect(url) • Write(variant)

  30. Response Object: Classification • Insert information • Write(), BinaryWrite() • Send cookie: Cookie • Redirecting: Redirect() • Buffering the page • Buffer, Flush(), Clear(), End() • Setting the properties of a page • Expires, ExpiresAbsolute, CacheControl, ContentType, AddHeader, Status

  31. Inserting Information • Response.Write(“string”) • Insert a string into the HTML output • Convert the text to an appropriate character set • <% = “string” %> • Response.BinaryWrite(data) • Prevent the conversion

  32. Sending Cookies • Response.Cookies(“CookieName”)=“data” • Response.Cookies(“CookieName”).Expires=“11/26/1197 17:35:00” • Response.Cookies(“CookieName”).Domain=“/netnt.mcu.edu.tw/” • Response.Cookies(“CookieName”).Path=“/u99” • Response.Cookies(“CookieName”).Secure=True • Multiple Value Cookie <% Response.Cookies(“CookieName”)(“item1”)=“data1” Response.Cookies(“CookieName”)(“item2”)=“data2” %>

  33. Redirecting the Browser • Refer users to alternative web pages • Redirection header • Tell the browser to go and get the information elsewhere • Usage • Response.Redirect(“URL”)

  34. Buffering the Page • An extra degree of control over • When a client receives information • What they receive • Usage • Response.Buffer = True ’Default is false • Response.Flush() ’Send the current content • Response.Clear() ’Clear the current buffer • Response.End() ’Stop processing and send • When reach the end, the contents are sent

  35. Server Object • The roof of the object model • Provides access to methods and properties on the server. • Most of these methods and properties serve as utility functions. • Property • ScriptTimeout: Amount of time a script can run • Method • CreateObject(progID) Create an instance of an object • HTMLEncode(string) HTML Encoding • URLEncode(string) URL Encoding • MapPath(path) Convert a virtual path to a physical path

  36. ScriptTimeout Property • Define the delay before all scripts are terminated • Default = 90 seconds • Usage • Server.ScriptTimout = nn;

  37. HTMLEncode Method • Replace the angle-brackets with an escape sequence • Server  Client <% = Server.HTMLEncode(“<Table>”) %> <% = Server.HTMLEncode(“<%=Server.ScriptTimeout %\>”) %>

  38. URLEncode Method • Convert a string into URL-encoded form • Space  + • Special chars  %nn <a href=“a1.asp?ans=<%server.urlencode(33%) %>”> 33 % </a> <a href=“a1.asp?ans=33%25”> 33% </a>

  39. MapPath Method • Provide file location information for use in scripts • Logical path  Physical path • Usage • PhyPath = Server.MapPath(“/clipper”) • e:\clipper • /path: virtual directory • path: relative path

  40. CreateObject Method • Invoke objects on the server • Extend the use of server components • Usage • Set obj = Server.CreateObject(“ProgId”) • Use the methods and access the properties of the object • IsObject( obj ) • Check if the object is created successfully

  41. <%@language=JScript%> <% textfile = Server.MapPath("/app5")+"\\test1.html" fsObject = Server.CreateObject("Scripting.FileSystemObject") outStream= fsObject.CreateTextFile(textfile, true, false) myString = "<HTML><HEAD><TITLE>File</TITLE></HEAD>" outStream.WriteLine(mystring) now=new Date() myString = "The time is " + now.toLocaleString() outStream.WriteLine(mystring) outStream.WriteLine("</BODY></HTML>") outStream.close() Response.Write("<A HREF='test1.html'>My New Text File </A>")%>

  42. Application Object • To share information among all users of a given application. • An ASP-based application is defined as all the .asp files in a virtual directory and its subdirectories. • Collections • Contents: Contains all of the items that have been added to the Application. • StaticObjects: Contains all of the objects added to the session with the <OBJECT> tag.

  43. Application Object (cont.) • Methods • Lock: Prevents other clients from modifying Application object properties. • Unlock: Allows other clients to modify Application object properties. • Events • Application_OnEnd : Occurs when the application quits • Application_OnStart : Occurs when a page is first referred. • Scripts for the preceding events are declared in the global.asa file.

  44. Application Object • Usage Application.Lock() Application(“name”)=“value” Application.Unlock() • Event handles: • global.asa in the root directory of the virtual mapping function Application_OnStart() { … } function Application_OnEnd() { … }

  45. Example - Application Object <%@language=JScript%> <% Application.Lock() Application("NumVisits") = Application("NumVisits") + 1 Application.Unlock() %> ... This application page has been visited <%= Application("NumVisits") %> times! Global.asa <script language=Jscript runat=server> function Application_OnStart(){ Application(“NumVisits”)=0; } </script>

  46. Application Object • Application Life • Start • The application starts the first time any client requests a document from that virtual mapping • End • The web server is stopped by the operating system

  47. appTest.html <FORM METHOD=POST NAME="Personal" ACTION="appTest.asp"> Please enter your name: <input type=text size=20 name="name" value=""><br> Please enter your age: < input type=text size=5 name="age" value=""><br> Please select which city your are living in: <SELECT NAME="city" ><P> <OPTION VALUE="Seattle">Seattle <OPTION VALUE="Denver">Denver <OPTION VALUE="Miami">Miami </SELECT><br> <INPUT TYPE=SUBMIT></FORM>

  48. appTest.asp <%@language=JScript%> <% Application.Lock() Application("name") = ""+Request.Form("name") Application("age") = ""+Request.Form("age") Application("city") = ""+Request.Form("city") Application.Unlock() %> <body> <h3>Hello <%= Application("name") %>, thank you</h3> <% if (Application("city") = = "Seattle") { %> The weather in <%= Application("city") %> is grey skies and plenty of rain. <% } else if (Application("city") = = "Denver") { %> The weather in <%= Application("city") %> is cold and snowy. <% } else { %> The weather in <%= Application("city") %> is warm and sunny. <% } %> </p> <form name=“age” method=“POST” action="app2.asp"> <p><input type="SUBMIT" value="OK"> </p>

More Related