880 likes | 1.01k Views
IT in Education e-Leadership & Management Series. Using Active Server Page (ASP) to create Simple Web-database Application. Speaker : PAU CHUNG WAI (Half-seconded Teacher). IT Management FWG’s Blog. http://edblog.hkedcity.net/itman. Reference Site. http://www.w3schools.com.
E N D
IT in Education e-Leadership & Management Series Using Active Server Page (ASP) to create Simple Web-database Application Speaker:PAU CHUNG WAI (Half-seconded Teacher)
IT Management FWG’s Blog http://edblog.hkedcity.net/itman
Reference Site • http://www.w3schools.com
Introduction to Dynamic Web Page • Dynamic means content on a web page can change in response to different context or conditions. • More interactive than static web page • Two ways: • Client-side Scripting (e.g. JavaScript, VBScript) • Server-side Scripting (e.g. PHP, Perl, ASP, JSP)
What is ASP? • ASP stands for Active Server Pages • ASP is a program that runs inside IIS • IIS stands for Internet Information Services • IIS comes as a free component with Win XP and Vista
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.
Web Server Request ASP Engine Response Client displayed in browser How does ASP differ from HTML?
Advantages of using ASP? • Dynamically edit, change or add any content of a Web page • Access any data or databases and return the results to a browser • Provide security since your ASP code cannot be viewed from the browser
How to install IIS in Win XP? • To run ASP, IIS must be installed. • Follow these steps to install IIS on Windows XP: • On the Start menu, click Settings and select Control Panel. • Double-click Add or Remove Programs. • Click Add/Remove Windows Components. • Click Internet Information Services (IIS). • Click Details. • Select the check box for World Wide Web Service, and click OK. • In Windows Component selection, click Next to install IIS. • After you have installed IIS, make sure you install all patches for bugs and security problems.
After Installation, you can find the IIS program icon in System Tools (系統管理工具):
Test your Web • Follow these steps after you have installed IIS: • Look for a new folder called Inetpub on your hard drive. • Open the Inetpub folder, and find a folder named wwwroot. • Create a new folder, “ASP", under wwwroot. • Copy all the sample files to this folder • Make sure your Web server is running. • Open your browser and type“http://localhost/ASP/testing.asp"to view your first web page.
How to install IIS in Windows Vista? • On the Start menu, click Settings and select Control Panel. • Select Turn Windows Features On or Off from Programs and Features. • Select Internet Information Services. Expand the tree nodes of IIS, check the options that you want to turn on. (You must turnon ASP feature) • Then click OK to install IIS.
TRUE How to install IIS in Windows Vista?
How to install IIS in Win Server 2003? • When you start the Windows Server 2003, you should see the Manage Your Server wizard • If the wizard is not displayed, go to Administrative Tools, and select Manage Your Server • In the wizard, click Add or Remove a Role, click Next • Select Custom Configuration, click Next • Select Application Server role, click Next • Select Enable ASP.NET, click Next • Now, the wizard may ask for the Server 2003 CD. Insert the CD and let it run until it is finished, then click the Finish button • The wizard should now show the Application Server role installed
How to install IIS in Win Server 2003? • Click on Manage This Application Server to bring up the Application Server Management Console (MMC) • Expand the Internet Information Services (IIS) Manager, then expand your server, and then the Web Sites folder • You should see the Default Web Site, and it should not say (Stopped) • IIS is running! • In the Internet Information Services (IIS) Manager click on the Web Service Extensions folder • Here you will see that Active Server Pages are Prohibited (this is the default configuration of IIS 6) • Highlight Active Server Pages and click the Allow button • ASP is now active!
Basic Syntax • ASP file normally contains HTML tags • Also contains server scripts, surrounded by the delimiter <% and %> • Server scripts are executed on the server • Results are plain HTML codes • Source codes not viewed by the web browser
Example 1A example1a.asp <html> <body> <% response.write(“Hello World!”) %> </body> </html>
Example 1B example1b.asp <html> <body> <% =“hello World!” %> </body> </html>
Example 2A example2a.asp <html> <body> <% response.write(“The time now is ” & now()) %> </body> </html>
Example 2B example2b.asp <html> <body> <% response.write(formatdatetime(now(),1) & "<br>") response.write(formatdatetime(now(),2) & "<br>") response.write(formatdatetime(now(),3) & "<br>") response.write(formatdatetime(now(),4) & "<br>") %> </body> </html>
Variable • A variable is used to store information. • 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.
Example 3 example3.asp <% X = “Hello World!” Sub Display() Dim X X = “Welcome!” Response.write(X) End Sub %> <html> <body> <% call display() %> <br> <% =X %> </body> </html>
Procedure • You can call both a JavaScript procedure and a VBScript procedure in an ASP file
Example 4 – Calling a VBScript Procedure example4.asp <html> <head> <% sub vbproc(num1,num2) response.write num1*num2 end sub %> </head> <body> <p> The result of 3 * 4 is <%call vbproc(3,4)%>. </p> </body> </html>
Example 5 – Calling a JavaScript Procedure example5.asp <%@ language=“JavaScript”%> <html> <head> <% function jsproc(num1,num2) { Response.Write(num1*num2) } %> </head> <body> <p> The result of 3 * 4 is <%jsproc(3,4)%>. </p> </body> </html>
Session Object • The Session object is used to store information about, or change settings for a user session. • Variables stored in the Session object hold information about one single user, and are available to all pages in one application.
Example 6 example6.asp <html> <head> <% Session(“Username”) = “Raymond” %> </head> <body> <p> My name is <%=session(“Username”)%>. </p> </body> </html> Keep the browser open, browse another ASP file called “example7.asp”.
Example 7 example7.asp <html> <head> </head> <body> <p> My name is <%=session(“Username”)%>. </p> </body> </html> Now, close all the browsers, Open “example7.asp” again. What do you see?
Session Object • A session ends if a user has not requested or refreshed a page for a specified period. (By default, this is 20 minutes) • You can set the timeout property: <% Session.Timeout=5 %>
Session Object • To end a session immediately, you may use the Abandon method: <% Session.Abandon %> Can you name one application of the Session object?
Application Object • 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. • The Application object in ASP is used to tie these files together.
Application Object • You can create Application variables in “Global.asa” <script language="vbscript" runat="server"> Sub Application_OnStart application("vartime")="" application("users")=1 End Sub </script>
What is Global.asa? • The “Global.asa” file is an optional file that can contain declarations of objects, variables, and methods. • It can be accessed by every page in an ASP application. • The “Global.asa” file must be stored in the root directory of the ASP application, and each application can only have one “Global.asa” file.
This “Global.asa” file counts the number of current visitors Example 8 Global.asa <script language="vbscript" runat="server"> Sub Application_OnStart Application("visitors")=0 End Sub Sub Session_OnStart Application.Lock Application("visitors")=Application("visitors")+1 Application.UnLock End Sub Sub Session_OnEnd Application.Lock Application("visitors")=Application("visitors")-1 Application.UnLock End Sub </script> Save this file in the root directory (i.e. c:\inetpub\wwwroot\)
To display the number of current visitors in an ASP file: Example 8 example8.asp <html> <head> </head> <body> <p> There are <%response.write(Application("visitors"))%> online now! </p> </body> </html>
The #include Directive • You can insert the content of one ASP file into another ASP file. • The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.
Example 9 example9.asp <html> <body> <h3>Words of Wisdom:</h3> <p><!--#include file="wisdom.inc"--></p> <h3>The time is:</h3> <p><!--#include file="time.inc"--></p> </body> </html>
Example 9 Here is the content of "wisdom.inc" file: "One should never increase, beyond what is necessary, the number of entities required to explain anything." Here is the content of “time.inc" file: <% Response.Write(Time) %>
User Input • Form is usually used to collect information from user. For example: <html><body><form action=“display.asp" method="get">Your name: <input type="text" name="fname" size="20"><input type="submit" value="Submit"></form></body></html> • Get or Post method could be used. • Action defines the ASP file that accepts the user inputs.
Request Object • The Request object may be used to retrieve user information from forms. For example: <html><body><form action="example10.asp" method="get">Your name: <input type="text" name="fname" size="20"><input type="submit" value="Submit"></form><%fname=Request("fname")If fname<>"" Then Response.Write("Hello " & fname & "!")End If%></body></html> example10.asp Let’s try another method - “Post”
Classwork 1 • Create the following ASP file that accepts name and sex as input, then output the text as indicated.