Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming."— Presentation transcript:

1 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming the Web Chapter 10

2 10-2 Learning Objectives Get familiar with ASP syntax Use Active Server Pages objects for web applications Learn how to collect information in forms Work with cookies Learn how to set up Server Side Include files

3 10-3 What is ASP? Microsoft’s Active Server Pages (ASP) technology is a server- side scripting environment. It’s used in creating server-side scripts to automatically perform difficult or repetitious Web management tasks in dynamic web pages. You can write ASP scripts to: –Edit, change or add content to a Web page on the fly –Customize a Web page to make it more useful for individual users –Respond to user queries or data submitted through HTML forms –Access any data or databases and return the results to a browser

4 10-4 What is ASP? (2) An ASP file is simply an HTML file that can contain text, HTML, XML, and scripts. The core of an ASP file is the script. To run ASP, you need Microsoft’s Internet Information Server which runs under Windows NT 4 and Windows 2000 or the Personal Web Server. Combine HTML and/or XML with a little VBScript or JavaScript, attach the.asp file extension to the file, and you’ve got an Active Server Page.

5 10-5 How ASP Works An ASP file can contain any combination of HTML, script, or commands. The script can assign values to variables, request information from the server, or combine any set of commands into procedures. It works like this: 1.A browser requests an ASP file from your web server 2.Internet Information Server passes the request to the ASP engine. 3.The ASP engine reads the ASP file, line by line, and executes any commands in the file 4.Finally, the ASP file is returned to the browser as plain HTML which is displayed on the page

6 10-6 Speaking the Language of ASP Active Server Pages (ASP) technology is language- independent. VBScript and JScript scripting languages are supported right out of the box. In fact, several scripting languages can be used within a single.asp file. VBScript is the default scripting language for ASP so if you omit the language directive the server simply assumes it’s VBScript. You can also use JavaScript as the scripting language by inserting it into the language directive at the top of the page:

7 10-7 ASP Syntax Welcome to My First ASP Page Welcome The time is now This sends the text to the browser The delimiters,, tells the web server that it must run the enclosed script code before the server transmits the page to the browser This tells the web server to create the entire web page in memory before sending it to the client

8 10-8 ASP Variables Typically in programming, a variable is used to store information and so it is in ASP. Two caveats are: –If the variable is declared outside a procedure it can be changed by any script in the ASP file. –If the variable is declared inside a procedure, it is created and destroyed every time the procedure is executed. To declare variables accessible to more than one ASP file, declare them as session variables or application variables. Session variables are used to store information about a single user, and are available to all pages in one application where an application is a collection of ASP pages.

9 10-9 ASP Procedures You can call a JavaScript procedure from a VBScript, and vice versa. When calling a VBScript or a JavaScript procedure from an ASP file written in VBScript, you can use the call keyword followed by the procedure name. <% Sub vbproc(num1,num2) Dim result result = num1 * num2 Response.Write(result) End sub %> 3 multiplied by 4 is:

10 10-10 Figure 10.1. Using JavaScript and VBScript Functions in One ASP Page function jsproc(num1,num2) { var sum; sum = num1 + num2; Response.Write(sum); } Sub vbproc(num1,num2) Response.Write(num1*num2) End sub Using JavaScript Function: 3 + 4 = Using VBScript Sub: 3 * 4 =

11 10-11 ASP Objects Most of the functionality you can build into an ASP page comes from objects on the server. These objects enable your pages to communicate effectively with the server, the application, the current session, and the user. In ASP, an object can have collections, properties, methods, and events. The Request and Response objects contain collections (bits of information that are accessed in the same way). Objects use methods to do some type of procedure and properties to store any of the object’s attributes (such as color, font, or size).

12 10-12 Uses for ASP Objects ASP objects are commonly used for chores such as: –Alerting users about relocated Web content by redirecting them using the ASP Redirect method to automatically redirect or route a browser to another Web page or Web site. –Monitoring user preferences and behavior by using ASP to place cookies to determine which part of the Web site a user is looking at and how long that user lingers on certain Web pages. –Obtaining user feedback by using the ASP Form and QueryString collections to gather user input from an HTML form. These collections simplify the creation Web sites that process user feedback, such as a departmental bulletin board, an online survey, or a data retrieval system.

13 10-13 ASP Objects (2) Five of the built-in ASP objects are commonly used for web application development: –Request Object –Response Object –Server Object –Session Object –Application Object

14 10-14 Request Object When a browser asks for a page from a server, it is called a request. The ASP Request object is used to get information from the user. The Request object contains all of the data sent to the web server when a browser makes a request. You can use the Request object to parse encoded URLs, access information from a form, and read cookies.

15 10-15 Figure 10.2. Collecting the Name of the User First Name:

16 10-16 Figure 10.3. Output of Welcome.asp

17 10-17 Figure 10.4. Output of RevisedWelome.asp Welcome <% Response.Write(Request.Form("txtName")) %>

18 10-18 Figure 10.5. Extracting Client’s and Server’s Environments Using Request.ServerVariables The requestor's browser environment is: <% Dim clientsEnv clientsEnv= Request.ServerVariables("HTTP_USER_AGENT") Response.Write(clientsEnv) Response.Write(" ") %> The server software is <% Dim serverEnv serverEnv=Request.ServerVariables("SERVER_SOFTWARE") Response.Write(serverEnv) %>

19 10-19 Figure 10.6. Displaying the List of Members in the Request.ServerVariables Collection All possible server variables: <% For Each Item in Request.ServerVariables Response.Write(Item & " ") Next %>

20 10-20 Response Object The ASP Response object is used to send output to the user from the server. When a user clicks a hyperlink or types an http address into the web browser, the browser sends a request to the web server. The web server processes the request and the server Response object transmits a response back to the browser.

21 10-21 Server Object and Session Object The ASP Server object is used to access properties and methods on the server. The Session object is used to remember information about a user’s session. A session is the time a user spends at your site on a visit. It begins when a user opens any.asp page and continues as the user navigates from page to page in the site. Session variables are commonly used to track things like the contents of a user’s shopping basket, or a flag indicating that this user has been properly authenticated.

22 10-22 Application Object A group of ASP files that work together to perform some purpose is called an application. The Application object in ASP is used to tie these files together. The purpose of the Application object is to store information that can be shared with all users of the application simultaneously. The Application object is used to store and access variables from any page, just like the Session object. The difference is that all users share one Application object, while with Sessions there is one Session object for each user.

23 10-23 ASP Cookies A cookie is a small file that the web server stores on the user’s computer to identify a user. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values using the Response.Cookies collection. You can use cookies, as well as session and application variables, to store and retrieve information. You can set cookie values with the Response object: <% Response.Cookies("firstname")="Valda" %>

24 10-24 Figure 10.7. Collecting Hobby Data From the User Please select a hobby Fishing Gardening

25 10-25 Figure 10.8. Storing Cookie Data <% Dim strHobby Dim dteExpireDate strHobby= Request.Form("radHobby") dteExpireDate=DateAdd("d", 30, Now) ' adds 30 days ' You can use DateAdd("n", 10, Now) to add 10 minutes, ' or use DateAdd("m",1,Now) to add 1 month ' Write the cookie and set its expiration date Response.Cookies("HobbyCookie")=strHobby Response.Cookies("HobbyCookie").Expires = dteExpireDate %> Thank you for providing the information

26 10-26 Figure 10.9. Redirection Based on the Cookie Data <% Dim strHobby strHobby = Request.Cookies("HobbyCookie") ' Retrieve the cookie ' Redirect the user to appropriate page Select Case strHobby Case "Fishing" Response.Redirect("Fishing.htm") Case "Gardening" Response.Redirect("Gardening.htm") Case Else Response.Redirect("AllProducts.htm") End Select %>

27 10-27 Figure 10.10. Retrieving the Values of a Multi-Valued Cookie <% Dim x,y For each x in Request.Cookies If Request.Cookies(x).HasKeys Then Response.Write("Multiple values in the " & x & " cookie are: ") For Each y in Request.Cookies(x) Response.Write(" "& y & "=" & Request.Cookies(x)(y) & " ") Next Else Response.Write(x & " = " & Request.Cookies(x)) End If Response.Write " " Next %>

28 10-28 Figure 10.11. The Output of ShowVisitCount.asp Number of visits since the server was started = <% Dim intCount intCount = Application("visitCount") Response.Write(" " & intCount) %>

29 10-29 Server-Side Includes The #INCLUDE directive lets you insert the content of another file into an ASP page before the server executes it. When ASP sees #INCLUDE, the contents of the file are placed into the ASP file in place of the #INCLUDE command. SSI is commonly used to store functions, headers, or any elements that will be reused on multiple pages. To insert a file into an.asp file, use the following syntax:

30 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. 10-30 The End


Download ppt "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming."

Similar presentations


Ads by Google