More Asp


Published on


Published in: Education
1 Comment
1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

More Asp

  1. 1. Windows 2000 and ASP as Development Environment Norman White 2001
  2. 2. IIS/ASP Integration with Office <ul><li>IIS and Office are tightly integrated in Office 2000 </li></ul><ul><li>Office applications can be used to directly generate Web applications, including Data Base applications </li></ul><ul><li>Integration makes it a great development environment for prototyping </li></ul><ul><li>But first, what is ASP? </li></ul>
  3. 3. Active Server Pages <ul><li>Concept is to have web pages that dynamically change (at the server) depending on external events (data, time, client, form inout etc). </li></ul><ul><li>ASP is Visual Basic Code embedded in the web page (like Javascript) and interpreted at the server before the page is delivered. </li></ul>
  4. 4. ASP Processing <ul><li>Unlike CGI processing and some other approaches, the ASP processor is part of the IIS web server (just as javascript is part of the browser. </li></ul><ul><li>This means that the OS does not have to start up another application on every page. </li></ul><ul><li>If the page has an ASP extension, the web server will look for embedded ASP code. </li></ul>
  5. 5. ASP coding <ul><li>Whenever the web server finds an ASP reference, it turns control over to ASP. The ASP processor executes the instructions and the returns control to the web server. Since the ASP processor is part of the web server, it can do things like generate HTML directly. </li></ul><ul><li>But it can also do almost anything you can do in VB, including data base access. </li></ul>
  6. 6. More ASP Forms <ul><li>Since ASP is part of the WEB server access to form information is much easier than most other approaches. </li></ul><ul><li>Var = request.form(“fieldname”); </li></ul><ul><ul><li>will place contents of field “fieldname” in Variable “Var” </li></ul></ul><ul><ul><li>(nicer than Unix shell huh?) </li></ul></ul>
  7. 7. ASP and Office <ul><li>Even better is the ability of Office to generate working ASP applications </li></ul><ul><li>Just click on “save as” and scroll down to the Active Server Pages option </li></ul><ul><ul><li>Office will generate a set of ASP files that implements the application on the WEB using ASP. </li></ul></ul><ul><li>Then move application to the web server and run </li></ul>
  8. 8. ASP and Databases <ul><li>ASP accesses data bases using ODBC (Open Data Base Connectivity ) now called ADO (ActiveX Data Objects) and MDAC </li></ul><ul><li>The name keeps changing, but the concept is the same </li></ul><ul><ul><li>Application Opens a data base connection to a local data object (which can point to remote data) </li></ul></ul><ul><ul><li>It then manipulates the data base </li></ul></ul>
  9. 9. ODBC Naming <ul><li>As long as the ODBC name is the same on the system you develop app and the web server, everything should run on the web server (or else you need to change the name) </li></ul><ul><li>To add an ODBC data source to a system, </li></ul><ul><li>Click on Start, Settings, Control Panel, ODBC Data Sources (32 bit) </li></ul><ul><li>Give the Data source a name, and then pick the database system, the server and the userid and password </li></ul>
  10. 11. Sample ASP file <ul><li><%@ Language=VBScript %> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Example 1</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><% </li></ul><ul><li>FirstVar = &quot;Hello world!&quot; </li></ul><ul><li>%> </li></ul><ul><li><%=FirstVar%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  11. 12. Looping in ASP <ul><li><%@ Language=VBScript %> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Example 2</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><% </li></ul><ul><li>FirstVar = &quot;Hello world!&quot; </li></ul><ul><li>%> </li></ul><ul><li><%FOR i=1 TO 10%> </li></ul><ul><li><%=FirstVar%> </li></ul><ul><li><%NEXT%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  12. 13. Functions in ASP <ul><li><%@ Language=VBScript %> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Example 3</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><% </li></ul><ul><li>FirstVar = &quot;Hello world!&quot; </li></ul><ul><li>%> </li></ul><ul><li>The time is: <%=time%> <BR> </li></ul><ul><li><%FOR i=1 TO 10%> </li></ul><ul><li><%=FirstVar%> </li></ul><ul><li><%NEXT%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  13. 14. Conditional Execution in ASP <ul><li>%@ Language=VBScript %> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Example 4</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><%IF Hour(time)>18 OR Hour(time)<4 THEN%> </li></ul><ul><li>Good Night Everyone. </li></ul><ul><li><%ELSE%> </li></ul><ul><li>Good Morning Everyone. </li></ul><ul><li><%END IF%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  14. 15. Sample Form (from Microsoft ASP Tutorial) <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Button Form</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><FORM NAME=&quot;Button Example&quot; METHOD=&quot;POST&quot; ACTION=&quot;tutorial/button.htm&quot;> </li></ul><ul><li>Computer Programming Experience: </li></ul><ul><li><P> </li></ul><ul><li><INPUT TYPE=&quot;button&quot; NAME=&quot;choice&quot; VALUE=&quot;Less than 1&quot;>Less than 1 year.<BR> </li></ul><ul><li><INPUT TYPE=&quot;button&quot; NAME=&quot;choice&quot; VALUE=&quot;1 to 5&quot;>1-5 years.<BR> </li></ul><ul><li><INPUT TYPE=&quot;button&quot; NAME=&quot;choice&quot; VALUE=&quot;More than 5&quot;>More than 5 years. </li></ul><ul><li></P> </li></ul><ul><li><P><INPUT TYPE=&quot;reset&quot; VALUE=&quot;Clear Form&quot;> </li></ul><ul><li><INPUT TYPE=&quot;submit&quot; VALUE=&quot;Submit&quot;> </li></ul><ul><li></P> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  15. 16. Guestbook Application <ul><li>Need to </li></ul><ul><ul><li>Create Database using Access </li></ul></ul><ul><ul><ul><li>I.e. create Table and fields </li></ul></ul></ul><ul><ul><li>Move Data Base to web server or create database in c:inetpubwwwrootTutorial </li></ul></ul><ul><ul><ul><li>(Default web server root) </li></ul></ul></ul><ul><ul><li>Create ODBC DSN for Database </li></ul></ul><ul><ul><li>Create ASP application (guestbook.asp) </li></ul></ul>
  16. 17. Guest Data Base fields <ul><li>Field Name </li></ul><ul><li>Data Type and General Properties </li></ul><ul><li>ID </li></ul><ul><li>AutoNumber, Field Size=Long Integer, New Values=Increment, </li></ul><ul><li>Indexed=Yes(No Duplicates) </li></ul><ul><li>TB1 </li></ul><ul><li>Text, Field Size=255, Required=No, Allow Zero Length=Yes, </li></ul><ul><li>Indexed=No </li></ul><ul><li>TB2 </li></ul><ul><li>Text, Field Size=255, Required=No, Allow Zero Length=Yes, </li></ul><ul><li>Indexed=No </li></ul><ul><li>TB3 </li></ul><ul><li>Text, Field Size=255, Required=No, Allow Zero Length=Yes, </li></ul><ul><li>Indexed=No </li></ul><ul><li>TB4 </li></ul><ul><li>Text, Field Size=255, Required=No, Allow Zero Length=Yes, </li></ul><ul><li>Indexed=No </li></ul><ul><li>MB1 </li></ul><ul><li>Memo, Required=No, Allow Zero Length=Yes </li></ul>
  17. 18. Creating ODBC Data Source <ul><li>1.In the ODBC Data Source Administrator, select the ODBC icon. </li></ul><ul><li>2.Select File DSN. </li></ul><ul><li>3.Select Add, select Microsoft Access Driver, and click Next. </li></ul><ul><li>4.Type in a descriptive name for your file DSN (Guestbook) and click Next. </li></ul><ul><li>5.Click Finish, click Select, specify the location of the database file, and select OK. </li></ul><ul><li>6.Click OK twice. After you specify the location of the database file, the ODBC Data Source Administrator creates a file DSN for it. </li></ul>
  18. 22. The Application Logic Guestbook.asp <ul><li>See if “Message” field (hidden) is True </li></ul><ul><ul><li>If true, retrieve field values and insert new entry into the database table </li></ul></ul><ul><li>Message not True </li></ul><ul><ul><li>If message is not true, generate the form </li></ul></ul><ul><ul><li>with a hidden field “message” = TRUE </li></ul></ul><ul><ul><li>and have the action statement run Guestbook.asp (this asp file </li></ul></ul><ul><li>Note, only one file </li></ul><ul><ul><li>It either processes the input, or generates the form for the input. </li></ul></ul>
  19. 23. Guestbook.ASP file (put in c:inetpubwwwrootTutorial) <ul><li><% @Language=VBScript %> </li></ul><ul><li><html dir=ltr> </li></ul><ul><li><head> </li></ul><ul><li><TITLE>Guest Book</TITLE> </li></ul><ul><li></head> </li></ul><ul><li><body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;> </li></ul><ul><li><% </li></ul><ul><li>IF request.form (&quot;Message&quot;)=&quot;True&quot; THEN </li></ul><ul><li>strTB1=request.form(&quot;To&quot;) </li></ul><ul><li>strTB2=request.form(&quot;EMailAdd&quot;) </li></ul><ul><li>strTB3=request.form(&quot;CC&quot;) </li></ul><ul><li>strTB4=request.form(&quot;Subject&quot;) </li></ul><ul><li>strMB1=request.form(&quot;Memo&quot;) </li></ul><ul><li>IF strMB1 = &quot;&quot; THEN </li></ul><ul><li>iLenMB1=255 </li></ul><ul><li>ELSE </li></ul><ul><li>iLenMB1 = Len(strMB1) </li></ul><ul><li>END IF </li></ul>
  20. 24. Create the Database connection <ul><li>‘ Connects to the Access driver and Access database in the Inetpub </li></ul><ul><li>‘ directory where the database is saved </li></ul><ul><li>strProvider = &quot;Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:InetpubWwwrootTutorialguestbook.mdb;&quot; </li></ul><ul><li>‘ Creates an instance of an Active Server component </li></ul><ul><li>set objConn = server.createobject(&quot;ADODB.Connection&quot;) </li></ul><ul><li>‘ Opens the connection to the data store </li></ul><ul><li>objConn.Open strProvider </li></ul><ul><li>‘ Instantiate Command object and use ActiveConnection property to </li></ul><ul><li>‘ attach connection to Command object </li></ul><ul><li>set cm = Server.CreateObject(&quot;ADODB.Command&quot;) </li></ul><ul><li>cm.ActiveConnection = objConn </li></ul>
  21. 25. Create an SQL Query <ul><li>‘ Define SQL query </li></ul><ul><li>cm.CommandText =&quot;INSERT INTO Guestbook (TB1,TB2,TB3,TB4,MB1) VALUES (?,?,?,?,?)&quot; </li></ul><ul><li>‘ Define query parameter configuration information for guestbook fields </li></ul><ul><li>set objparam=cm.createparameter(, 200, , 255, strTB1) </li></ul><ul><li>cm.parameters.append objparam </li></ul><ul><li>set objparam=cm.createparameter(, 200, , 255, strTB2) </li></ul><ul><li>cm.parameters.append objparam </li></ul><ul><li>set objparam=cm.createparameter(, 200, , 255, strTB3) </li></ul><ul><li>cm.parameters.append objparam </li></ul><ul><li>set objparam=cm.createparameter(, 200, , 255, strTB4) </li></ul><ul><li>cm.parameters.append objparam </li></ul><ul><li>set objparam=cm.createparameter(, 201, , iLenMB1, strMB1) </li></ul><ul><li>cm.parameters.append objparam </li></ul><ul><li>cm.execute </li></ul><ul><li>response.write(&quot;Thank you!&quot;) </li></ul>
  22. 26. Handle New Entries <ul><li>ELSE%> </li></ul><ul><li><h1>Guestbook</h1> </li></ul><ul><li><!--Post information to Guestbook form --> </li></ul><ul><li><form name=guestbook.asp action=&quot;guestbook.asp&quot; method=&quot;POST&quot;> </li></ul><ul><li><p>To</p> </li></ul><ul><li><p><input type=&quot;Text&quot; name=&quot;To&quot;></p> </li></ul><ul><li><p>Email Address</p> </li></ul><ul><li><p><input type=&quot;Text&quot; name=&quot;EmailAdd&quot;></p> </li></ul><ul><li><p> CC</p> </li></ul><ul><li><p><input type=&quot;Text&quot; name=&quot;CC&quot;></p> </li></ul><ul><li><p>Subject</p> </li></ul><ul><li><p><input type=&quot;Text&quot; name=&quot;Subject&quot;></p> </li></ul><ul><li><p>Message</p> </li></ul><ul><li><p><textarea name=&quot;Memo&quot; rows=6 cols=70></textarea></p> </li></ul><ul><li><input type=&quot;HIDDEN&quot; name=&quot;Message&quot; value=&quot;True&quot;> </li></ul><ul><li><input type=&quot;submit&quot; value=&quot;Submit information&quot;> </li></ul><ul><li></form> </li></ul><ul><li><%End if%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  23. 27. So, How hard is that? <ul><li>Not too bad, but how does someone look at the guestbook? </li></ul><ul><li>Need another ASP file to view entries </li></ul><ul><li>Nice to allow users to sort entries... </li></ul>
  24. 28. Viewing Program <ul><li><% @Language=VBScript %> </li></ul><ul><li><html dir=ltr> </li></ul><ul><li><head> </li></ul><ul><li><title>View Guest Book</title> </li></ul><ul><li></head> </li></ul><ul><li><body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;> </li></ul><ul><li><% </li></ul><ul><li>‘ This section makes it possible for visitors to sort the data in the columns in ascending order. </li></ul><ul><li>if request.form(&quot;sort&quot;)<> &quot;&quot; THEN </li></ul><ul><li>StrSort=request.form(&quot;sort&quot;) </li></ul><ul><li>ELSE </li></ul><ul><li>StrSort=&quot;TB1 ASC&quot; </li></ul><ul><li>END IF </li></ul>
  25. 29. Connect to Guestbook and create query <ul><li>strQuery=&quot;SELECT * FROM Guestbook ORDER BY &quot; &StrSort </li></ul><ul><li>‘ Database path statement describing the driver to use and the path to the desired database. </li></ul><ul><li>strProvider = &quot;Driver=Microsoft Access Driver (*.mdb); DBQ=C:InetpubWwwrootTutorialguestbook.mdb;&quot; </li></ul><ul><li>IF Request(&quot;ID&quot;) <> &quot;&quot; THEN </li></ul><ul><li>strIDNum=Request(&quot;ID&quot;) </li></ul><ul><li>‘ Creates an instance of an Active server component </li></ul><ul><li>set objConn = server.createobject(&quot;ADODB.Connection&quot;) </li></ul><ul><li>‘ Opens the connection to the data store </li></ul><ul><li>objConn.Open strProvider </li></ul><ul><li>‘ Instantiate Command object and use ActiveConnection property to </li></ul><ul><li>‘ attach connection to Command object </li></ul><ul><li>set cm = Server.CreateObject(&quot;ADODB.Command&quot;) </li></ul><ul><li>cm.ActiveConnection = objConn </li></ul>
  26. 30. <ul><li>‘ Define SQL query </li></ul><ul><li>cm.CommandText = &quot;DELETE FROM Guestbook WHERE ID = &quot; &strIDNum </li></ul><ul><li>cm.Execute </li></ul><ul><li>END IF </li></ul><ul><li>‘ Instantiate a Recordset object and open a recordset using </li></ul><ul><li>‘ the Open method </li></ul><ul><li>Set rst = Server.CreateObject(&quot;ADODB.recordset&quot;) </li></ul><ul><li>rst.Open strQuery, strProvider </li></ul><ul><li>%> </li></ul>
  27. 31. <ul><li><h1>Guest Book</h1> </li></ul><ul><li><form name=viewdb.asp action=viewdb.asp method=post> </li></ul><ul><li><table border=1 cellspacing=3 cellpadding=3 rules=box> </li></ul><ul><li><% </li></ul><ul><li>ON ERROR RESUME NEXT </li></ul><ul><li>IF rst.EOF THEN </li></ul><ul><li>Response.Write &quot;There are no entries in the database.&quot; </li></ul><ul><li>ELSE%> </li></ul><ul><li><tr> </li></ul>
  28. 32. <ul><li><% </li></ul><ul><li>‘ Deletes rows from the database, this cannot be undone </li></ul><ul><li>Response.Write &quot;<td width=200><center>Delete Record</center></td>&quot; </li></ul><ul><li>FOR i = 1 to rst.Fields.Count -1 </li></ul><ul><li>Response.Write &quot;<td width=200><input name=sort value=&quot; & rst(i).Name & &quot; type=submit></td>&quot; </li></ul><ul><li>NEXT </li></ul><ul><li>WHILE NOT rst.EOF %> </li></ul><ul><li><tr> </li></ul><ul><li><% </li></ul><ul><li>Response.Write &quot;<td align=left valign=top bgcolor=‘#ffffff’><a href=viewdb.asp?id=&quot; & rst(0) & &quot;>Delete</a></td>&quot; </li></ul><ul><li>FOR i = 1 to rst.fields.count - 1 </li></ul><ul><li>Response.Write &quot;<td align=left valign=top bgcolor=‘#ffffff’>&quot; & rst(i) &&quot;</td>&quot; </li></ul><ul><li>NEXT </li></ul><ul><li>rst.MoveNext </li></ul><ul><li>WEND </li></ul><ul><li>END IF </li></ul><ul><li>%> </li></ul><ul><li></table> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  29. 33. What is happening? What does the code do? <ul><li>Hint, What do you think the name of the asp file is? </li></ul>
  30. 34. Conclusion <ul><li>Win2K fast development environment </li></ul><ul><li>Integrated with Office </li></ul><ul><li>BUT </li></ul><ul><li>Win2K still not a mature platform </li></ul><ul><li>Needs </li></ul><ul><ul><li>Something like a standard shell language </li></ul></ul><ul><ul><li>Batch queues with priorities </li></ul></ul><ul><ul><li>Priority for different applications etc. </li></ul></ul><ul><li>Excellent system for small – medium businesses. </li></ul><ul><li>Still problematical for very large sites, but moving fast. </li></ul>