1 / 55

Benefits of ASP

Benefits of ASP. Active Server Pages (ASP) is a server side scripting language that it lets you transform the plain, static web site into a dazzling, dynamic solution.

abram
Download Presentation

Benefits of 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. Benefits of ASP • Active Server Pages (ASP) is a server side scripting language that it lets you transform the plain, static web site into a dazzling, dynamic solution. • With Microsoft's server side scripting language you can gather data from your site's visitors, use sessions, cookies, application variables, and more

  2. Is ASP a Complete Solution? • While ASP is quite useful, it is not a stand alone solution. • Rather, ASP is a supplement to HTML (and CSS and even Javascript) and your final ASP code will often contain bits of pieces of code that are not ASP. • If you had ever wanted to take orders, gather emails, or make a decent guestbook, ASP will provide you the necessary tools to complete these tasks.

  3. What is ASP? • ASP stands for Active Server Pages • ASP is a program that runs inside IIS • IIS stands for Internet Information Services • ASP is a Microsoft Technology • An ASP file is just the same as an HTML file • An ASP file can contain text, HTML, XML, and scripts • Scripts in an ASP file are executed on the server • An ASP file has the file extension ".asp"

  4. How Does ASP Differ from HTML? When a browser requests an HTML file, the server returns the file When a browser requests an ASP file, IIS passes the request to the ASP engine. The ASP engine reads the ASP file, line by line, and executes the scripts in the file. Finally, the ASP file is returned to the browser as plain HTML

  5. The Web Server find the file and then processes all the ASP code between <% ... %> before handing back the page. Code between <% ... %>never arrives at the browser Sent to browser from server <html><head><TITLE>hi.asp</TITLE></head><body bgcolor="#FFFFFF">Today is Tue 10:30am and all is well<br>Good Morning</body></html> Before 12 PM Original asp file on server <html><head><TITLE>hi.asp</TITLE></head><body bgcolor="#FFFFFF">Today is <%=now%> and all is well<br><%if hour(now())>12 THEN%>Good Evening<%ELSE%>Good Morning!<%END IF%></body></html> <html><head><TITLE>hi.asp</TITLE></head><body bgcolor="#FFFFFF">Today is Tue 02:00pm and all is well<br>Good Day!</body></html After 12PM

  6. What can ASP do for you? • Dynamically edit, change or add any content of a Web page • Respond to user queries or data submitted from HTML forms • Access any data or databases and return the results to a browser • Customize a Web page to make it more useful for individual users • The advantages of using ASP instead of CGI and Perl, are those of simplicity and speed • Provide security since your ASP code can not be viewed from the browser • Important: Because the scripts are executed on the server, the browser that displays the ASP file does not need to support scripting at all!

  7. Running an ASP Web Page • Open up "My Computer" or "Windows Explorer" so that you can view your system's files and directories. • the wwwroot folder - The full path to this location is "C:\Inetpub\wwwroot" • Inside the wwwroot folder create a New Folder and rename it “MyASP" • To access this directory you would type the following into Internet Explorer: • http://localhost/MyAsp/FILENAME.asp • Where FILENAME is name of your ASP file

  8. firstscript.asp ASP Code: <% Response.Write("Hello Me") %> Launch Internet Explorer and type the following into the address bar: http://localhost/MyASP/firstscript.asp Internet Explorer Display: Hello Me

  9. ASP Code: <html> <body> <% Response.Write("Hello Me") %> </body> </html> ASP - Tags These tags <% and %> will always encapsulate your ASP code. An opening ASP tag is <% while an HTML tag normally looks like <tagname> A closing ASP tag looks like %> while an HTML tag normally looks like </tagname> ASP code can occurr anywhere, even within an HTML tag opening tag like this: <a href="<% Response.Write("index.asp") %>">Home</a> anchor=a It display: Home

  10. ASP Programming - VBScript • ASP uses VBScript as its default scripting language. VBScript is similar to Javascript, a client side programming language used to add functionality through the <script> tag.

  11. Read the following paragraph carefully! • VBScript, when used to program ASP converts it to server side scripting. • Any given web page could be created from a combination of server side and client side scripting. • The confusing part is this: You must use these client side scripting languages (Javascript, VBScript, etc) to program in ASP!

  12. Server Side ASP Code using VBScript <html> <body> <%Dim myString myString = Date() Response.Write("The date is: " & myString) %> </body> </html> Display The date is: 9/23/2007

  13. ASP Code with VBScript and Client Side Javascript Code: <script> document.write("The date is: <% Dim myString myString = Date() Response.Write(myString) %>“) </script> Display: The date is: 9/23/2007 If you just want to program in ASP you can disregard the above example if you find it confusing. Just remember that you can have client-side Javascript code and server-side ASP/VBScript code included in an ASP generated web page!

  14. Programming ASP in Javascript VBScript is the default scripting language that ASP is coded in, so if you want to specify a different scripting language you have to state which scripting language you will be using at the very beginning of your code. Below is the line of code that must be your first line of ASP <%@ Language="javascript" 'The rest of your ASP Code....%>

  15. ASP OperatorsASP is programmed in VBScript by default, thus ASP's operators are VBScript operators by default. Operators in ASP fall into four categories Math, Comparisons, the somewhat more advanced Logic operators, and Leftovers(those that don't fit well into any category). ASP Arithmetic Operators The mathematical operators in ASP are similar to many other programming languages. However, ASP does not support shortcut operators like ++, --, +=, etc.

  16. Comparison Operators Comparison operators are used when you want to compare two values to make a decision. Comparison operators are most commonly used in conjunction with "If...Then" and "While something is true do this..." statements, otherwise known as conditional statements. The items that are most often compared are numbers. The result of a comparison operator is either TRUE or FALSE. There is no == or != operators.

  17. Logical Operators The above comparison operators result in a truth value of TRUE or FALSE. A logical operator is used for complex statements that must make decisions based on one or more of these truth values.

  18. String Operators & The only string operator is the string concatenation operator "&" that takes two strings and slams them together to form a new string. An example would be string1 = "Tim" and string2 = " is a Hero". The following code would combine these two strings into one: string3 = string1 & string2

  19. If Statement Syntax • ASP's If Statement is slightly different than the If Statement implementation in most other languages. • There are no brackets, or curly braces, nor are there any parenthesis. • Rather the beginning of the code to be executed in the If Statement when its true is marked with Thenand the end of the If Statement is plainly marked with End If. <% Dim myNum myNum = 6 If myNum = 6 Then Response.Write("Variable myNum = 6") End If %>

  20. If Else Conditional Statement <% Dim myNum myNum = 23 If myNum = 6 Then Response.Write("Variable myNum = 6") Else Response.Write("**Variable myNum = " & myNum) End If %>

  21. ASP Select Case Example The variable that appears immediately after Select Case is what will be checked against the list of case statements. These case statements are contained within the Select Case block of code.. ASP Code: <% Dim myNum myNum = 5 Select Case myNum Case 2 Response.Write("myNum is Two") Case 3 Response.Write("myNum is Three") Case 5 Response.Write("myNum is Five") Case Else Response.Write("myNum is " & myNum) End Select %>

  22. Question is where does this code run? On client or server! <html> <body> <script type="text/vbscript"> document.write("Scripts in the body section are executed when the page is loading") </script> </body> </html>

  23. Declaring a Variable in ASP It is a good programming practice to declare all your variables before you use them, even though it is not required. Nearly all programming languages require you to declare variables and doing so also increases your program's readability. In ASP you declare a variable with the use of the Dim keyword, which is short for Dimension. Variables can be declared one at a time or all at once. <% 'Single Variable Declarations Dim myVar1 Dim myVar2 'Multiple Variable Declarations Dim myVar6, myVar7, myVar8 %>

  24. ASP Variable Naming Conventions • ASP uses VBScript by default and so it also uses VBScripts variable naming conventions. These rules are: • Variable name must start with an alphabetic character (A through Z or a through z) • Variables cannot contain a period • Variables cannot be longer than 255 characters (don't think that'll be a problem!) • Variables must be unique in the scope in which it is declared (Basically, don't declare the same variable name in one script and you will be OK).

  25. ASP - Assigning Values to ASP Variables ASP Code: <% 'Single Variable Declarations Dim myString, myNum, myGarbage myNum = 25 myString = "Hello" myGarbage = 99 myGarbage = "I changed my variable" Response.Write("myNum = " & myNum & "<br />") Response.Write("myString = " & myString & "<br />") Response.Write("myGarbage = " & myGarbage & "<br />") %> Display: myNum = 25 myString = HellomyGarbage = I changedmy variable

  26. ASP ArraysASP Arrays are Zero Based! Declare <% Dim myFixedArray(3) 'Fixed size array of 4 Dim myDynArray() 'Dynamic size array %> Assign <% Dim myFixedArray(3) 'Fixed size array myFixedArray(0) = "Albert Einstein" myFixedArray(1) = "Mother Teresa" myFixedArray(2) = "Bill Gates" myFixedArray(3) = "Martin Luther King Jr." %>

  27. ASP Dynamic Sized Arrays To create an array whose size can be changed at any time simply do not put a number within the parenthesis when you declare the array. When you know what size you want the array to be use the ReDimkeyword. You may ReDim as many times as you wish. If you want to keep your data that already exists in the array then use the Preserve keyword. ASP Code: <% Dim myDynArray() 'Dynamic size array ReDim myDynArray(1) myDynArray(0) = "Albert Einstein" myDynArray(1) = "Mother Teresa" ReDim Preserve myDynArray(3) myDynArray(2) = "Bill Gates" myDynArray(3) = "Martin Luther King Jr." For Each item In myDynArray Response.Write(item & "<br />") Next %> Display: Albert Einstein Mother Teresa Bill Gates Martin Luther King Jr.

  28. Sub() and func() <html> <% 'this is to show how sub routine is called in VBS sub mySub() response.write("This is a sub procedure when called") end sub %> <body> <p>A sub procedure does not return a result.</p> <%call mySub()%> </body> </html>

  29. function procedure CAN return a result <html> <head> <% ‘this is to show function call in VBS. For run on server function myFunction() myFunction = "BLUE" end function %> </head> <body> <% response.write("My favorite color is " & myFunction())%> <p>A function procedure CAN return a result.</p> </body> </html>

  30. function call run on browser <html> <head> <script type="text/vbscript"> ‘function call to run on browser function myFunction() myFunction = "BLUE" end function </script> </head> <body> <script type="text/vbscript"> document.write("My favorite color is " & myFunction()) </script> <p>A function procedure CAN return a result.</p> </body> </html>

  31. ASP Components Object properties are their settings or characteristics. Properties can be read and written to access or change their settings. Object methods are their actions. These methods are used to activate the object to perform processing functions. Object collections are their data sets. Certain objects encapsulate sets of data that are used within processing operations

  32. ASP Built-in Objects In addition to the scripting and data access components there are five built-in ASP objects that provide functionality for the Web page author: • Response Object. is used by a script to send information from the server to the browser. Information appearing on a Web page is produced by the Response Object. • Request Object. is used to access data sent from the browser, most often transmitted from forms appearing on the Web page. • Session Object. is used to manage user sessions. A session is established when the user first arrives at a Web site and Session Objects can be used to track activities of users as they navigate through the site. • Application Object. is used to manage the entire Web application. It is used to keep track of users or to maintain information pertinent to all visitors to the • Server Object. is the mechanism through which VBScript creates File System Objects, Connection Objects, Recordset Objects, and other objects need by the script

  33. What are the ASP objects good for? • Much of the scripting necessary to process data on the server involves calling upon these ASP objects to read or set their properties, activate their methods, or access their collections of data. • These objects provide a wide range of functionality and frees the Web developer from coding detailed instructions to accomplish the tasks

  34. Recall ASP

  35. ASP Session • The Session Object in ASP is a great tool for the modern web site. • It allows you to keep information specific to each of your site's visitors. • Information like username, shopping cart, and location can be stored for the life of the session so you don't have to worry about passing information page to page.

  36. When does a Session Start? A session starts when: A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure A value is stored in a Session variable A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope

  37. When does a Session End? A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes. If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property. The example below sets a timeout interval of 5 minutes: <% Session.Timeout=5 %>

  38. ASP Session Variables To store a Session Variable you must put it into the Contents collection, which is very easy to do. Here we are saving the Time when someone visited this page into the Session Contents collection and then displaying it . <% 'Start the session and store informationin session variable Session("TimeVisited") = Time() Response.Write("You visited this site at: " & Session("TimeVisited")) %>

  39. ASP Session ID The ASP Session ID is the unique identifier that is automatically created when a Session starts for a given visitor. The Session ID is a property of the Session Object and is rightly called the SessionID property. Below we store the user's SessionID into a variable. <% Dim mySessionID mySessionID = Session.SessionID Response.Write(Session.SessionID) %> To end a session immediately, you may use the Abandon method: <% Session.Abandon %>

  40. Problem with sessions is WHEN they should end • Note: The main problem with sessions is WHEN they should end. We do not know if the user's last request was the final one or not. • So we do not know how long we should keep the session "alive". • Waiting too long for an idle session uses up resources on the server, but if the session is deleted too soon the user has to start all over again because the server has deleted all the information. Finding the right timeout interval can be difficult!

  41. Store and Retrieve Session Variables The most important thing about the Session object is that you can store variables in it. The example below will set the Session variable username to "Donald Duck" and the Session variable age to "50": <% Session("username")="Donald Duck" Session("age")=50 %> When the value is stored in a session variable it can be reached from ANY page in the ASP application: Welcome <%Response.Write(Session("username"))%> The lineabove returns: "Welcome Donald Duck".

  42. Remove Session Variables The Contents collection contains all session variables. It is possible to remove a session variable with the Remove method. The example below removes the session variable "sale" if the value of the session variable "age" is lower than 18: <% If Session.Contents("age")<18 then  Session.Contents.Remove("sale") End If  %> To remove all variables in a session, use the RemoveAll method: <% Session.Contents.RemoveAll() %>

  43. ASP “Request” Object and Forms The Request.QueryString and Request.Form commands may be used to retrieve information from forms, like user input. This is HTML form <form method="GET" action=“testRequest.asp"> Name <input type="text" name="Name"/> Age <input type="text" name="Age"/> <input type="submit" /> </form>

  44. Request.QueryString using GET method Content of “testRequest.asp” file that retrieves data field from form using GET <% Dim name, age name = Request.QueryString("Name") age = Request.QueryString("Age") Response.Write("Name: " & name & "<br />") Response.Write("Age: " & age & "<br />") %> test.asp testRequest.asp http://localhost/testRequest.asp?Name=Fred&Age=52

  45. ASP Form Post The previous lesson ASP Form Get created an ASP page to process information sent through an HTML form with the GET method. In this lesson we will be examining how to process data sent via the POST method and see the difference. <form method="POST" action=“postTest.asp"> “test.html” with post Name <input type="text" name="Name"/> Age <input type="text" name="Age"/> <input type="submit" /> </form> File postTest.asp content is: post processor asp file <% Dim name, age name = Request.Form("Name") age = Request.Form("Age") Response.Write("Name: " & name & "<br />") Response.Write("Age: " & age & "<br />") %> http://localhost/postTest.asp‘there is no data in url when POST is used A query string is a set of name/value pairs appended to the end of a URL Same output as in GET

  46. GET vs POST • Using "get" to pass information sends the information appended to the request for the processing page. It tends to be simpler and you can troubleshoot any problems simply by looking at the address bar in your browser since all values passed are displayed there. This is also the primary weakness of this method. The data being passed is visible and is limited in size to the maximum length of a request string. • Using "post" to pass information sends the information embedded in a header during the request for the processing page. It's main advantage is that you can send larger amounts of information. It also doesn't make that information visible in the address bar of the browser which is nice if you are using the "hidden" input type. The value of this type is still readily available to the user by using view source, but the average user won't see it or be confused by any information you may need to pass from your form for processing • So the only difference between a GETand POST processor is replacing all instances of QueryStringwith Form in the asp post processor file. • A query string is a set of name/value pairs appended to the end of a URL

  47. ASP Cookies • Like ASP Sessions, ASP Cookies are used to store information specific to a visitor of your website. \ • This cookie is stored to the user's computer for an extended amount of time. • If you set the expiration date of the cookie for some day in the future it will remain their until that day unless manually deleted by the user.

  48. ASP Create Cookies Creating an ASP cookie is exactly the same process as creating an ASP Session. Once again, you must create a key/value pair where the key will be the name of our "created cookie". The created cookie will store the value which contains the actual data. we will create a cookie named brownies that stores how many brownies we ate during the day. Create a cookie <% 'create the cookieuse Response object Response.Cookies("brownies") = 13 %> Retrieve a cookie <% Dim myBrownie 'get the cookie use Request object myBrownie = Request.Cookies("brownies") Response.Write("You ate " &myBrownie & " brownies") %>

  49. ASP Cookie Expiration Date Unlike real life cookies, in ASP you can set how long you want your cookies to stay fresh and reside on the user's computer. A cookie's expiration can hold a date; this date will specify when the cookie will be destroyed. In our example below we create a cookie that will be good for 10 days by first taking the current date then adding 10 to it. <% 'create a 10-day cookie Response.Cookies("brownies") = 13 Response.Cookies("brownies").Expires = Date() + 10 'create a static date cookie Response.Cookies("name") = "Suzy Q." Response.Cookies("name").Expires = #January 1,2009# %>

  50. ASP Cookie Arrays or Collections Up until now we store one variable into a cookie, which is quite limiting if you wanted to store a bunch of information. However, if we make this one variable into a collection it can store a great deal more. Below we make a brownies collection that stores all sorts of information. <% 'create a big cookie Response.Cookies("brownies")("numberEaten") = 13 Response.Cookies("brownies")("eater") = "George" Response.Cookies("brownies")("weight") = 400 %>

More Related