1 / 21

Introduction to Server-Side Scripting in .ASP

This course provides an overview of server-side scripting in .ASP, including the advantages, available script languages, and the use of server-side includes. It also covers CGI, ASP models, and the basics of VBScript.

bodin
Download Presentation

Introduction to Server-Side Scripting in .ASP

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. ASP: Course 1 An introduction to server side scripting

  2. Server-side Scripting • The advantage to server side scripting is that it works with any browser. • The choice of script languages available is quite large: • PERL (From Unix) • Java (From Sun Microsystems) • JavaScript (As used in DHTML) • VBScript (From Microsoft) • Others (Lots) ASP - 1

  3. Server-side includes • Include code works with most web servers. • Must use correct file extension (.asp or .shm on IIS). Some implementations support almost any extension. Some don’t. • Standard headers and footers • <!-- #include file="seltag.inc”--> • (From C preprocessing directives) • A version of server-side includes is generated by FrontPage. ASP - 1

  4. Server Side Includes - II • Server side includes are good for: • Adding automatic headers and footers • Adding standard script libraries • Adding definitions, styles and snippets • The Virtual keyword is used to point the inclusion from the standard point “\winnt\system32\inetsrv” (is the default) • Path names are relative to that point. ASP - 1

  5. Other preprocessing directives • #FLASTMOD last file modified date • #FSIZE file size • #CONFIG • can be used to dynamically change configuration settings such as stream formats, etc. • #ECHO used to output server side variables. Handy for debugging. • #EXEC execute a system command (CGI, CMD or ISA) ASP - 1

  6. Server-side models • CGI model • Code with HTML (also FoxWeb). • Programs generate HTML when passed request information. • ASP/Fusion • HTML with embedded code. • Microsoft’s ASP solution will be focus of several lectures. • Server-side work objects • JavaBeans, MTX objects, 3rd party server-side tools like discussion engines, etc. ASP - 1

  7. What is CGI? • Common Gateway Interface • Server-side programs or operating system supported scripts (on Unix PERL is the most popular). • Typical request: http://myserver/cgi-bin/myScript?Arg1=X&Arg2=Y • CGI argument format is the basis for ASP. ASP - 1

  8. CGI and How it works For forms whose action property is 'GET' the form elements are sent as parameters after the ? Separated by & For forms whose action property is 'POST' the form elements are sent as part of the message content to the server. <form method='___' action='http://myServer/cgi-bin/myScript'> </form> (Script can also be a binary program on the server) 1 3 /cgi-bin/myScript myServer STDOUT: Returned as HTML to Caller Page on Client 2 Submit CGI Scripts receive POST data as STDIN: Link 1 2 http://myServer/cgi-bin/myScript?Arg1=X&Arg2=Y Arguments (after the ?, passed as arguments to script) ASP - 1

  9. Introduction to ASP • ASP is nifty because the output can be made to work on any browser. Also, ASP is being ported to lots of platforms. • How to add script <SCRIPT LANGUAGE="VBScript" RunAt="Server"> • Some sources endorse <!-- and --> to enclose script blocks but since the code is being interpreted on the server side, where you know it works, this seems pointless. Commonly use <% and %> to enclose code blocks. • Should use </SCRIPT> when done. ASP - 1

  10. VBScript - I: Variables • All variables in VBScript are variant types. • Most common problem with ASP pages is the run time error (RTE) “type mismatch” resulting from “Involuntary Type Conversion” (ITC). If you always use explicit type conversion via conversion functions you can avoid this problem. Always assume the worst case. Code is cheap, RTE's are expensive. • Use the correct delimiters for strings ("" quote) and dates (# # pound sign). • To deal with nulls use IsNull() function or add & "" to convert Nulls to Strings. ASP - 1

  11. List of verbs • Dim/Redim - make a variable/define an array • Const - define a constant • while...wend, for...each... next, for...next - Loops • if…then, Select Case - Decision making • sub/function - Subprogram parts • Set - Object reference operator • Math and String operators (particularly do NOT use + (Mathematical PLUS) for & (String concatenation); this leads to bad trouble ITC or RTE. ASP - 1

  12. Built-in functions • See the reference http://msdn.microsoft.com/scripting/vbscript/doc/vbstoc.htm • CINT, CSTR, etc. - Type conversion • Len(), Space(), String(), etc. - String Functions • FormatNumber, FormatCurrency, etc. - format other data types for output. • Date, DateDiff(), DatePart(), etc. - Date/Time Functions • Exp(), Eval(), Sin(), etc. - Math Functions. • Other - See the web page. ASP - 1

  13. ASP vs. VB • Be aware that many functions do not work the same from VB/VBA to VBScript. • Some important ones are missing, like the generic Format$() function. • Some have different arguments. • Some are special to VBScript such as all of ScriptEngineXXX() functions. ASP - 1

  14. Overview Client Response Server Object Application Object Request Session Object ObjectContext From Prof. Server Pages (WROX) p. 83 ASP - 1

  15. Intrinsic objects overview • Use these whenever possible, cause less overhead. • Request - Exposes what client sent to server. • Response - Collects output back to client. • Application - Represents the ASP page itself. • Session - represents a continuity of connection. • Server - provides services to the application. • ObjectContext - For MTX and transactions. ASP - 1

  16. Create-able Components • These are good, but call as few as you need, open as short as possible, reuse in same page if possible. • FileSytemObject • TextStreamObject • ADODB • Browser Capabilities component • Other ASP - 1

  17. ASP Sample - #1 <html> <head> <title>ASP Example 1</title> </head> <body> <script Language="vbscript" runat="server"> Response.Write("The date is " & date ) </script> </body> </html> ASP - 1

  18. ASP Sample #2 <html> <head> <title>ASP Example 2</title> </head> <body> <script Language="vbscript" runat="server"> dim i For i = 1 to 4 response.write("<p><Font Size=" & cStr(i) & ">Font Size of " & cStr(i) & "</font></p>") Next </script> </body> </html> Notice: in examples we will sometimes break lines at delimiters, it should be typed in as one continuous line! ASP - 1

  19. ASP Example #3 <script Language="vbscript" runat="server"> dim i dim j response.write("<h2>Multiplication Table</h2>") response.write("<table border='1' width='60%'>") response.write("<tr>") for i = 0 to 5 if i = 0 then response.write("<td width='15%' align='right'>&nbsp;</td>") else response.write("<td width='15%' align='right'><strong>" & cStr(i) & "</strong></td>") end if next response.write("</tr>") for i = 1 to 5 response.write("<tr>") for j=0 to 5 if j=0 then response.write("<td width='15%' align='right'><strong>" & cStr(i) & "</strong></td>") else response.write("<td width='15%' align='right'>" & cStr(i*j) & "</td>") end if next response.write("</tr>") next response.write("</table>") </script> ASP - 1

  20. ASP Example 4 <script Language="vbscript" runat="server"> dim i dim s randomize i=int(rnd(1)*10)+1 select case i case 1: s = "Your life will suck, then you'll die" case 2: s = "Ask Again" case 3: s = "No more Mr. Nice ASP Page" case 4: s = "So this is Christmas, and what have you done." case 5: s = "You can't see my code ha ha" case 6: s = "Good fortune will follow you... and steal you wallet." case 7: s = "No! Good Student, Sit!" case 8: s = "My God! There are Stars, Stars Inside." case 9: s = "Open the Pod Bay Door, HAL." case else s = "Sorry, dave. I can't do that." end select response.write("<h3>Magic ASP Page Says: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & s & "</h3>") </script> ASP - 1

  21. ASP Example 5 <script Language="vbscript" runat="server"> dim objBC set objBC=Server.CreateObject("MSWC.BrowserType") Response.write("</h3>Browser Capibilities Component</h3>") Response.Write("<p>" & objBC.Browser & " " & objBC.Version & "</p>") if objBC.ActiveXControls then Response.Write("<p>&nbsp;&nbsp;&nbsp;" & "[x] Supports Active X Controls</p>") if objBC.vbscript then Response.Write("<p>&nbsp;&nbsp;&nbsp;" & "[x] Supports vbScript</p>") if objBC.frames then Response.Write("<p>&nbsp;&nbsp;&nbsp;[x] Supports Frames</p>") set objBC = nothing </script> Note: For all that we set, we must forget, with NOTHING! ASP - 1

More Related