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
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
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
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
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.
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).
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.
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
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.
10-15 Figure 10.2. Collecting the Name of the User First Name:
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) %>
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 %>
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.
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.
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.
10-24 Figure 10.7. Collecting Hobby Data From the User Please select a hobby Fishing Gardening
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
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 %>
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 %>
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) %>
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: