Server Side Programming Database Integration Peter O’Grady Server Side Programming ASP 1 Content Introduction Server Side Programming (ASP) Database Integration Server Side Programming ASP 2 Introduction Internet allows for client-server systems Programs can be invoked on either/both clients and servers: – Client side includes Java Applets, ActiveX components with JScript or VBScript. – Server side includes CGI and ASP Data can be kept on servers distributed across network and accessed via client (topic of this portion of course) Server Side Programming ASP 3 Why use Internet to access data? People can access the data using a familiar browser interface. No special software required. Data can be maintained centrally or distributed. Data can be integrated with other information in an institution’s web pages Information can be made available to anyone (with permission) on the Internet Server Side Programming ASP 4 How can database be accessed by web browser? Server Side Programming e.g. Microsoft IIS server : ASP (VBScript, JScript, SQL) Database Internet Web browser Web browser Web browser Server Side Programming ASP 5 Client Side Scripts They are browser-specific, so the same code may be interpreted differently depending on the browser being used. (Note recent use of ECMAScript as general JScript or JavaScript) They can't be used to refer to a database (such as a product catalog). They can lessen the burden on server and are particularly useful for such tasks as validating data before sending to server side to be processed. Server Side Programming ASP 6 Server Side Programming (ASP) Server Side Programming ASP 7 Content Server Side programming overview Example – Microsoft Internet Information Services (IIS) – Active Server Page(ASP) – VBScript/JScript Server Side Programming ASP 8 Common Gateway Interface (CGI) CGI Is a standard for interfacing external applications with web servers (Common Gateway Interface, 1995). Involves a new process each time it is invoked therefore can be slow with multiple users. Alternatives: ISAPI (ASP), NSAPI. Server Side Programming ASP 9 IIS IIS is a web server that runs on Win NT, 2000, Server2003 and XP Server. Can use component-based (COM/ActiveX) client/server applications as well as JScript (ECMAScript) and VBScript Server Side Programming ASP 10 What is ASP? Runs on IIS "a server - side scripting environment that you can use to create and run dynamic, interactive, high - performance Web server applications”. Microsoft Can include COM/ActiveX components and/or compiled code Server Side Programming ASP 11 What are Active Server Pages? Server-side environment Can provide compile-free application environment Combine HTML, scripts, COM and ActiveX server components. Server Side Programming ASP 12 Using ASP User usually completes HTML form in a browser and submits this to a .asp file on the server (note that you need a .asp file on the server) .asp file contains both HTML and scripts/components Scripts/components invoked and produce new HTML code resulting page: original HTML + new HTML (generated by asp) - this is sent to client. Server Side Programming ASP 13 Using ASP Server evaluates tag starting with <% and ending with %> Scripts/components contained within tags Web servers supporting ASP: – Personal Web Server (Windows 95, 98, NT) – Internet Information Services (Windows NT Server, 2000 Server, Server 2003, XP) Server Side Programming ASP 14 ASP Application Dell Computers – http://www.dell.com Barnes and Noble Book Store – http://www.bn.com Pricescan – http://www.pricescan.com Server Side Programming ASP 15 ASP Basics ASP file is a text file with the extension .asp it contains any combination of: – Text – HTML Tags – ASP Script Commands/Components Server Side Programming ASP 16 ASP Script ASP Script could be VBScript or Jscript (ECMAScript) A script is a series of commands or instructions. Script command instructs the web server to perform an action. VBScript is similar to Visual Basic and Visual Basic for Application (VBA). Server Side Programming ASP 17 VBScript Basics Not case sensitive Declaring Variables VBScript does not require variable declarations, but it is good scripting practice to declare all variables before using them. To declare a variable in VBScript, use the Dim, Public, or Private statement. Server Side Programming ASP 18 Declaring Variables Dim: Declares variables and allocates storage space. Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure. Ex. Dim A,B Private: Used at script level to declare private variables and allocate storage space. Private variables are available only to the script in which they are declared Public: Used at script level to declare public variables and allocate storage space. Variables declared using the Public statement are available to all procedures in all scripts in all projects. Server Side Programming ASP 19 VBScript Basics VBScript Operators: – Arithmetic: +, -, *, /, ^ – Comparison: =, <>, <, >, <=, >= – Logical (for Boolean variables): Not, And, Or, Xor Server Side Programming ASP 20 VBScript Basics Conditional Statement: – IF … Then… Else • If condition Then [Statement] • elseif condition-n Then [elseifstatment]… • else [elsestatements] • End if Server Side Programming ASP 21 VBScript Basics Loop: – Do … Loop – While …Wend – For … Next – For Each …Next Procedures: – Sub – Function Server Side Programming ASP 22 ASP Demo ASP uses <% and %> to enclose script commands. <html> <head> <title>ASP Example 1</title> </head> <body> <p>This page was last refreshed on <%= Date %> </p> <p>Now it is <%= Now %> </p> </body> </html> Note 1: The VBScript function Now returns the current time, Date returns current date. Note 2: No data is input by the user Server Side Programming ASP 23 Running ASP Save file with .asp extension on server Client accesses the file in similar fashion to .htm (or .html) file Server detects .asp extension and runs script within ASP tags Results sent to client http://128.255.21.191/Example1/Example1.asp Server Side Programming ASP 24 Client-Server Structure Running ASP Script... IIS request results Client Server Side Programming ASP 25 Example 2 <html> <head> <title>ASP Example 2</title> </head> <body> <form method="POST" action="resultofexample2.asp"> <p>A= <input type="text" name="ValueofA" size="20"></p> <p>B= <input type="text" name="ValueofB" size="20"></p> <p><input type="submit" value="Calculate" name="B1"></p> </form> </body> </html> http://128.255.21.191/Example2/Example2.htm Server Side Programming ASP 26 Example 2 see web site Contained in HTML code: <%@ language="vbscript" %> <% dim A, B A=Request.form("ValueofA") B=Request.form("ValueofB") %> ………. <p>A+B= <%= eval(A)+eval(B) %> </p> <p>A-B= <%= eval(A)-eval(B) %> </p> <p>A*B= <%= eval(A)*eval(B) %> </p> ……. Server Side Programming ASP 27 example 2 asp <html> <head> <title>Example 2 ASP Page</title> </head> <body> <p>Example Page <%@ language="vbscript" %> <% dim A, B A=Request.form("ValueofA") B=Request.form("ValueofB") %> </p> <p>A+B= <%= eval(A)+eval(B) %> </p> <p>A-B= <%= eval(A)-eval(B) %> </p> <p>A*B= <%= eval(A)*eval(B) %> </p> </body> </html> Server Side Programming ASP 28 Database Overview Server Side Programming ASP 29 What is a Database? A database is an organized collection of related data, typically stored on disk, and accessible by possibly many concurrent users. Most common is relational database, which is a tabular database in which the data is defined so that it can reorganized and accessed in a number of different ways Server Side Programming ASP 30 Database Management Systems DBMS A program to access, manipulate and present the data to a user. Example DBMS: – Oracle – Sybase (Microsoft SQL) – IBM’s DB2, IMS and SQL/DS – dBase – Access Server Side Programming ASP 31 Relational Database Structure set of tables containing data fitted into predefined categories. each table(relation) contains one or more data categories in columns. each row contains a unique instance of data for the categories defined by column. Server Side Programming ASP 32 Example: Access Relational database Fairly good GUI. Can use SQL Server Side Programming ASP 33 Relationships in Access Multiple tables in one database Matching key fields between table – A key is usually a field with the same name in both tables. – Such key is primary key for one table, foreign key in the other table. Server Side Programming ASP 34 SQL SQL: Structured Query Language It is used to query from and update database. Systems using SQL: • • • • Oracle Sybase Microsoft SQL server Access Server Side Programming ASP 35 SQL Standard SQL commands: – Select – Insert – Update – Delete – Create Server Side Programming ASP 36 SQL Example database - example3.mdb tablename - table1 columnname - ID, ItemName, Price Server Side Programming ASP 37 SQL Example To find out the ‘Price’ when ‘ItemName’ equals to Candy SELECT Price FROM Table1 WHERE ItemName = ‘Candy’ Note: Can SELECT * to return whole row as an object - see Example 3 on website Server Side Programming ASP 38 SQL Basic Insert tablename (fieldname1, fieldname2…) values (value1, value2) update tablename set column_name1=expression1 Server Side Programming ASP 39 SQL Basic Create tablename (fieldname1 type, fieldname2 type) Delete [*] from tablename where clause (deletes whole row) Server Side Programming ASP 40 Database Integration Requirements Server (IIS) Database • Internet database connector (ODBC) not always required can use ADODB Dynamic Web Page (asp) • • • • User Input using HTML forms Data Transmission Retrieving data from database Display results Server Side Programming ASP 41 Operation ASP database Server HTML Client Server Side Programming ASP 42 Using web browser to access a database Web pages and forms are used to provide access to the database. Database can be queried or updated. Platform-independent front-end. Database vendor-independent front-end Client machine does not need database networking software Server Side Programming ASP 43 Example Implementation Step 1: HTML file (containing forms) for user input on server. Step 2: Database on server Step 3: Setup database connection (ODBC) Server Side Programming ASP 44 Step I: HTML File with Forms Example: <form method="Post" action="Find.asp"> <p> <select name = "item" method ="post" size ="1"> <option selected value="Gum">Gum</option> <option selected value="Suckers">Suckers</option> <option selected value="Taffy">Taffy</option> <option selected value="Skittles">Skittles</option> <option selected value="M&Ms">M&Ms</option> <option selected value="Lifesavers">Lifesavers</option> <option selected value="Snickers">Snickers</option> </select><input type="submit" value="Tell me the price"> </p> </form> This captures user input and sends result to result.asp Server Side Programming ASP 45 Server Side Programming ASP 46 HTML Forms Example http://128.255.21.191/Example3/Example3.htm Server Side Programming ASP 47 Step II: Database Under the home directory, create the access database named “Candy.mdb” tablename - Products columnname - ID, ItemName, Price Server Side Programming ASP 48 <%@ LANGUAGE='VBSCRIPT'%> <html> <head> </head> <body> <% dim price Set MyConn = Server.CreateObject("ADODB.Connection") set rs = Server.CreateObject("ADODB.Recordset") MyConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath ("Candy.mdb") item = Request.form("item") Set RS = MyConn.execute("Select * from Products where Candy = '" & item & "'") if rs.Eof then Price = "" else Price = RS("Price") End if Response.Write("It will cost you $" & price) Set MyConn = nothing Set rs = nothing %> </body> </html> Server Side Programming ASP 49 Running ASP Check the extension (asp) server evaluates tag starting with <% and ending with %> server replaces the tags with normal HTML using scripts results page: original Markup + new Markup(generated by asp) Server Side Programming ASP 50 Result Server Side Programming ASP 51 Java2 Enterprise Edition Server Side Programming ASP EJBs (Enterprise JavaBeans) ----picture PCWeek 52