230 likes | 363 Views
Active Server Page. 1.What is ASP 2.Create, Process Forms 3.Display, Edit Databases 4.Built-in Objects. Part I: What is ASP?. An abbreviation for Active Server Pages Free and already built into Win2000
E N D
Active Server Page 1.What is ASP 2.Create, Process Forms 3.Display, Edit Databases 4.Built-in Objects
Part I: What is ASP? • An abbreviation for Active Server Pages • Free and already built into Win2000 • Using server-side scripting (The code inside ASP is mixed in with standard HTML and is NEVER seen by the browser.) • Allows you to dynamically generate browser-neutral content
Web Browser Database Windows NT Server Part I: What is ASP? • Runs on Microsoft Internet Information Server (IIS) only • Administrated with FrontPage Explorer • Extension of .asp • Default language: VBScript • CGI, PHP, JSP….
Part I: What is ASP? • A simple example • The Web Server finds the file and then processes all the ASP code between <% ... %> before handing back the page. Code between <% ... %> never arrives at the browser. <html> <head> <title>hi.asp</title> </head> <body bgcolor="#FFFFFF"> <p>Today is 3/1/00 12:22:54 AM and all is well<br> Good Day! </p> </body> </html> Today is 3/1/00 12:22:54 AM and all is wellGood Day! <%@ Language=“VBScript”%> <html><head><TITLE>hi.asp</TITLE></head><body bgcolor="#FFFFFF">Today is <%=now%> and all is well<br><%if hour(now())>13 THEN%>Good Morning<%ELSE%>Good Day!<%END IF%></body></html>
Part II: Process Form Input in ASP • Forms are the primary way that a user feeds information to the server and collected by ASP objects. A form is a web page that contains tags that cause the browser to show fields that the user can fill in. • Forms can be HTML or ASP files. The form must pass the variables onto a .asp file to process the form. • With Get or Post Method • Get sends form input to the browser as part of the URL E.g., http://lw4fd.law4.hotmail.msn.com/cgi-bin/ HoTMaiL?disk=216.33.148.69_d514&login=sungchichu& f=33792&curmbox=ACTIVE&_lang=&fti=yes • Post sends form input to the browser as part of the Request • Tips: a text link to post above form would look like this:<a href= "javascript:submitform(document.forms['whatever'])">whatever</a>
there is a limit on the number of characters (approximately 4,000 but varies depending on server and browsers involved. The form will show its parameter in the browser address window, for example:testform.asp?state=md&city=Germantownwould be the URL in the browser, if the state and city field were populated Part II: Process Form Input in ASP • It supports many more characters than get (megabytes of data in case of file uploads) • The form will not show its parameter in the browser address window, for example:http://whatever.com/testform.aspwould be the only URL in the browser, regardless of how many fields and how much data is passed. • Form with Post • <form action="x.asp" name="whatever" method="post">....<input type=submit><input type=reset></form> • An ASP script picks up the form field with:<%whatever=request.form("whichfield")%> • Form with GET • <form action="x.asp" name="whatever" method=get>....<input type=submit><input type=reset></form> • An ASP script picks up a form field with:<%whatever=request.querystring("whichfield")%>
<html><head><title>FormTextBox.asp</title></head><body bgcolor="#FFFFFF"><Form action = "FormTextBoxRespond.asp" method="get">Fill Out This Form For Us:<p>Last Name -> <Input NAME="NameLast" size ="10"><br>Country -> <Input NAME="Country" value="USA" size=10><br>State -> <Input NAME="State" MaxLength="2" size=2><br><Input type="submit" value="Give me your data!"><hr></form></body></html> <html><head><title>FormTextBoxRespond.asp</title></head><body bgcolor="#FFFFFF"><%lname=request.querystring("namelast")cty=request.querystring("country")st=request.querystring("state")response.write lname & "<br>"response.write cty & "<br>"response.write st & "<br>"%></body></html> Part II: Process Forms in ASP
Part II: Create, Process Forms Leo USA 20
Part II: Create, Process Forms • IF Statement • Very often you must determine what to do next based on user input. This is one of the roles of the IF statement. First we make a form that will ask a user for their first name and last name • If … then else (elseif) end if • <html><head><TITLE>ifrespond.asp</TITLE></head><body bgcolor="#FFFFFF"><%fname=request.querystring("Firstname")lname=request.querystring("Lastname")If fname="George" and lname="Washington" then%>Hi.<p>You must be the first president!<%else%>Hi!<p>Nice to Meet You<%end if%></body></html>
Part II: Create, Process Forms • Select Case statement • Using IF-THEN can be cumbersome, prone to programmer errors and slower to execute. A more efficient construct is SELECT CASE (Switch-break in C?). It is optimized for testing one variable against many conditions • Here is the select case that will determine what the form input means. • <%fname=request.querystring("Firstname")lname=request.querystring("Lastname")%>Nice to Meet You <%=fname%><%=lname%><p><% select case lcase(lname)case "washington","adams"response.write "The first president has same last name<p>"case "jefferson"response.write "The third president has same last name<p>"case "lincoln"response.write "The sixteenth president has same last name<p>"end select%>
Part III: Display, Edit Databases • ActiveX Data Objects(ADO) Active Server Page ADO OLE DB Layer SQL Relational Database ODBC data Provider 3rd party data provider None-relational data store
Part III: Display, Edit Databases • ADODB.Command • ADODB.Connection • ADODB.Error • ADODB.Field • ADODB.Parameter • ADODB.Property • ADODB.Recordset Set adoCon= Server.CreateObject(“ADODB.Connection”) Set adoRec= Server.CreateObject(“{ADODB.Recordset”) adoCon.Open “Yourdatabase” adoRec.activeConnection=adoCon adoRec.open SQLstatement ….. adoRec.close adoCon.close set adoRec = nothing set adoCon = nothing
Recordset Object Methods AddNew MoveFirst / MoveLast / Move Previous Open Update Delete Property EOF / BOF CursorType ActiveConnection AbsolutePosition Connection Object Methods Close Execute Open Property Attributes CommandTimeout ConnectionTimeout Mode Part III: Display, Edit Databases
Part III: Display, Edit Databases • display a table from a SQL statement. • <% set conntemp=server.createobject("adodb.connection")conntemp.open myDSNmySQL="select * from publishers where state='NY'"set rstemp=conntemp.execute(mySQL)If rstemp.eof thenresponse.write "No records matched<br>"response.write mySQL & "<br>So cannot make table..."connection.closeset connection=nothingresponse.endend if%><table border=1><% response.write "<tr>"for each whatever in rstemp.fieldsresponse.write "<td><B>" & whatever.name & "</B></TD>"nextresponse.write "</tr>"
Part III: Display, Edit Databases • DO UNTIL rstemp.eofpubid=rstemp("pubid")name=rstemp("name")company_name=rstemp("company_name") ...… cellstart="<td align=""top"">"response.write "<tr>"response.write cellstart & pubid & "</td>"response.write cellstart & name & "</td>"response.write "</tr>"rstemp.movenextLOOP%></table><%' Now close and dispose of resourcesrstemp.closeset rstemp=nothingconntemp.closeset conntemp=nothing%>
Part III: Display, Edit Databases • Select field1, field2, ... from table1, table2, … where selection_criteria • Insert into table_name (fields, …) Values (values…) • Insert into table_name Select Select_statement • Update table_name set field1=new value, field2=new value… where… • Delete from table_name where… Be Careful!
Part IV: Built-in Objects • There are six kinds of intrinsic objects that make up the IIS object model:Application, ObjectContext, Request, Response, Server, Session • Each has its own Properties, Collections, Methods, Events
Part IV: Built-in Objects • Request: gives you access to the user’s HTTP request header and body • By working with form, create dynamic web pages and perform more meaningful server-side actions(such as updating a database) based on input from the user • Properties: TotalBytes • Collections: ClientCerficate, Cookies, Form, QueryString, ServerVariables • Methods: BinaryRead • Events: None
<form action=“http://mycorp.com/secure.asp” method=post> <input type=“file” name=“filename”> <br> <input type=“submit” value=“OK”> </form> <% Dim lngTotalByteCount Dim vntRequestData lngTotalByteCount=Request.TotalBytes vntRequestData=Request.BinaryRead(lngTotalByteCount) %> Part IV: Built-in Objects
Part IV: Built-in Objects • Request.Form has three properties of its own • Key: Represents the name of a specific element of the Form • Item: Represents the value of a specific element of the Form • Count: Returns the number of elements in the collection • strKeyName=Request.Form.Key(3) • strKeyValue=Request.Form.Item(strKeyName) • Request.Form(str) is just an abbreviated form of Request.Form.Item(str)
Part IV: Built-in Objects • Response: gives you a great deal of control over the HTTP response to the client • Properties: Buffer, CacheControl, Charset, ContentType, Expires, ExpiresAbsolute, IsClientConntected, PICS, Status • Collections: Cookies • Methods: Addheader, AppendToLog, BinaryWrite, Clear, End, Flush, Redirect,, Write • Event: None
Part IV: Built-in Objects • The following is the same: • <% Response.write “Your name is” Response.write Request.Form(“name”) Response.write “<br>” %> • Your name is <%=Request.Form(“name”)%><br>
Part IV: Built-in Objects • An example using Server Object • <html><head><TITLE>bc.asp</TITLE></head><body bgcolor="#FFFFFF"><% Set bc = Server.CreateObject("MSWC.BrowserType") %>Browser Name: <%=bc.browser %><p> Browser Version: <%=bc.version%><p><% if (bc.frames = TRUE) then %>I noticed you do frames<p><% else %>I noticed you are frame challenged<p><% end if %><% if (bc.tables = TRUE) then %>I noticed you do tables<p><% else %>I noticed you can't do tables<p><% end if set bc=nothing%></body></html>