53
1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

Embed Size (px)

Citation preview

Page 1: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

1Web and DB - INT Evry

Web and databases

Bruno DEFUDEComputer Science Dept

Page 2: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

2Web and DB - INT Evry

WWW – Web advantages

Universal client Easy to use Open standards Good integration with other Internet services and

protocols Extensible Low software and network costs Corporate network (Intranet), inter-companies

network (extranet), WAN

Page 3: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

3Web and DB - INT Evry

Why coupling web and DB?

Web is a kind of DB but» Without schema» Without query language» Without transactions, recovery, …» Without powerfull authorisation mechanism

DBs store huge amount of data which are interesting to publish on the web

Page 4: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

4Web and DB - INT Evry

Simple coupling

Transform DBs into sets of static web pages» Simple» Redundency, problem of consistency» Disadvantages of web

Need to generate dynamic web pages constructed with DB content

Page 5: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

5Web and DB - INT Evry

First solution: CGI scripts

Browser (client)

HTTPserver

DBMS

url

Html result

CGI

querystring

SQLHTML

Page 6: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

6Web and DB - INT Evry

Pros and Cons of CGI

Simple and portable Lots of code to write (one CGI per

query!): can be improved with generic solutions

Not very efficient

Page 7: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

7Web and DB - INT Evry

WWW – CGI problem

Client 1

Client 2

Client 3

C

G

I

Server Process

Process 1

Process 1

Process 1HTTP Server

CGI Scripts

Page 8: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

8Web and DB - INT Evry

WWW – the API Solution

Client 1

Client 2

Client 3

Server Process

HTTP Server

API

Set of functions

Thread 1

Thread 2

Thread 3

CGI scripts are functions of a DLL, Processed As threads within the multi-threaded HTTP server

Page 9: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

9Web and DB - INT Evry

FastCGI Persistent CGI script CGI (daemon like) A fastcgi script is splitted into 3 parts:

» init : process one time» body : process at each request» ending : process one time

Init part must include costly part of the CGI code (connection to a DBMS, …)

Well adapted to DBMS access, but is persistent (can not handle numerous different connections)

Page 10: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

10Web and DB - INT Evry

WWW – Web limitations

performances» Internet network => increase bandwith» CGI scripts => FastCGI, server-side scripts

security» HTTP => S-HTTP secure» TCP/IP level => SSL protocol

Page 11: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

11Web and DB - INT Evry

WWW – Web Limitations (2)

Transaction management» not possible with HTTP 1.0

– no session mode– One can use "cookies"

» Possible with HTTP 1.1– Allows persistent connexion (TCP level)

User interface» HTML is not very powerfull to construct sophisticated

UI» Java is more powerfull

Page 12: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

12Web and DB - INT Evry

WWW – Web evolutions

At the beginning» access to informations

– Distributed on the network– Represented by hypermedia documents

today» Software construction and processing

– client/server, heterogeneous– Inside the same organization (Intranet)

Page 13: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

13Web and DB - INT Evry

Coupling Web and DBMS

Gateways Principles Java approach (applets, servlets, JSP) Server-side scripting (PHP)

Page 14: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

14Web and DB - INT Evry

Gateways Principles

Single-request gateways Multiple-request gateways

(transactional)

Page 15: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

15Web and DB - INT Evry

Functionalities

(1) translate HTTP request (mapping environment variables to SQL statements)

(2) process SQL queries on the DBMS (3) encode SQL results as HTML pages

Page 16: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

16Web and DB - INT Evry

Process SQL queries

Program written using a DBMS API» DBMS-dependent approach (embedded

SQL)» DBMS-independent approach (ODBC,

JDBC or DBI for Perl) Programming languages used

» classical : C, C++, Ada, … if embedded SQL

» Scripting language : Perl (with DBI)

Page 17: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

17Web and DB - INT Evry

HTML encoding Generic solution (can be automated):

» select : HTML table» other query : string

Specific solution: a specific program for each query

– Within the gateway (one gateway / query !)– Within the DBMS (one stored procedure / query)

Intermediate solution:» encoding can be parametrized (update form,

hypertext navigation within the DB ,…)

Page 18: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

18Web and DB - INT Evry

DB access (simple html)

Browser (client)

HTTPserver

DBMS

url

Html result

CGI

Gateway

querystring

SQL data

HTML

Page 19: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

19Web and DB - INT Evry

Simple HTML example

<html><body><form name="f1" action="http://mica/multi2.cgi" method="get"><input type="hidden" name="uid" value="citcom/citcom@MICA"><input type="hidden" name="sqlstatement"

value="select * from students where sname=">Give a name : <input name="sname" value=""><p><input type="submit" value="lancer"></form></body></html>

Page 20: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

20Web and DB - INT Evry

DB access (client-side script)

Browser (client)

HTTPserver

DBMS

url

Html result

gatewayquerystring

SQL data

HTML

Page 21: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

21Web and DB - INT Evry

HTML with Javascript example

<html><body><form name="f1" action="http://mica/multi2.cgi" method="get"><input type="hidden" name="uid" value="citcom/citcom@MICA"><input type="hidden" name="sqlstatement"

value="select * from students where sname=">Give a name : <input name="sname" value=""><p><input type="button" value="go"

onClick="f1.sqlstatement.value+=f1.sname.value; f1.submit();">

</form></body></html>

Page 22: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

22Web and DB - INT Evry

DB access (without http)

Browser (client)+ java, TCL program (applet, tclet …)

DBMS

Specificprotocol

Specific protocol: JDBC, IIOP (CORBA)

Page 23: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

23Web and DB - INT Evry

Functionalities

(1) (2) (3)

Simple HTML

Client-sidescript

without http

SpecificCGI DBMS

DBMS

DBMS

gateway orDBMS

gateway orDBMS

client

client

Page 24: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

24Web and DB - INT Evry

Variations

DBMS-dependent or independent (native or ODBC)

Supported query language (SQL or subset, static vs dynamic)

HTML encoding (generic or specific) efficiency

Page 25: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

25Web and DB - INT Evry

Efficiency

CGI vs NSAPI, ISAPI (but proprietary solutions)

Decrease process number (if large number of clients) : multithreaded gateway

Page 26: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

26Web and DB - INT Evry

Transactions

transaction = DB program (read and write sequence)

ACID properties– A : Atomicity– C : Consistency– I : Isolation– D : Durability

Page 27: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

27Web and DB - INT Evry

Examples of transactional services

on-line ordering (music, books, planes, …)

bank insurance e-business!!!!

Page 28: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

28Web and DB - INT Evry

Transactions and Web

Transaction = sequence of URL invocations (management context by the web?)

Transaction = ACID properties (ensured by the DBMS)

HTTP = no session support– Client-side management context (cookies)– Simulation of HTTP sessions (transactional web)– Use server-side scripting languages (PHP, ASP, JSP, ...)

Page 29: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

29Web and DB - INT Evry

Transactions and cookies

Browser (client) http server cgi

DBMS

1 11’ c11’ c1

2 c1 2 c12’ c1, c2 2’ c1, c2

3 fin c1, c2 3 fin c1, c2

4 sql(c1, c2)

5’ del(c1, c2) 5’ del(c1, c2)4’ ok1 : first access

1’ : c1 cookie is generated2 : other access with c1 cookie transport2’ : c1, c2 cookies are generated3 : end of transaction with c1, c2 transport4 : transaction is constructed and processed on the DBMS5’ : cookies deletion

Page 30: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

30Web and DB - INT Evry

Transaction and cookies (2)

Context is managed on client-side (cookies)

advantages:– Easy to code– DB access is done one time at the end of the

session (DB resources are not blocked)– If no explicit user termination, nothing to do (at

DB level)

Page 31: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

31Web and DB - INT Evry

Transaction and cookies (3)

Cons– Not really transactional– Limited functionality– Lots of cgi scripts to code

Cookies problems– global to a user (no distinction between two

windows)– global to a url (does not allow two different

transactions on the same site at the same site)

Page 32: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

32Web and DB - INT Evry

transactional gateway

browser (Client) http Server cgi daemon gateway DBMS

Page 33: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

33Web and DB - INT Evry

transactional gateway principles

Context is managed by a daemon on server-side

Need of a transaction id stored on client-side (cookie or rewritten URL) and on server-side (in an array of the daemon)

A gateway does not process a single query but a complete transaction (a sequence of queries)

Page 34: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

34Web and DB - INT Evry

transactional gateway operations (1)

1 : transaction beginning request (implicite or explicite)

– An id is allocated by the daemon, transactions array is updated, a gateway is launched, an id is send back to the client (cookie or rewritten URL)

2 : DB operation request – The request is routed by the daemon on the

right gateway using the transaction id and the transactions array

Page 35: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

35Web and DB - INT Evry

transactional gateway operations(2)

3 : transaction ending request– implicit : error, timeout

– explicit : idem 2 + update of transactions array, id is deleted on client-side and the gateway is stopped

Page 36: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

36Web and DB - INT Evry

transactional gateway Resume

Pros– really transactional– generic solution

Cons– complex architecture – DB resources are blocked until the end of the

transaction– Need to detect a user « abort » (timeout)

Page 37: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

37Web and DB - INT Evry

Scripting language (server-side)

Offer a programming language integrated to the web (ability to have calls from a web page, associated to a HTTP server)

run-time of the language offers session support (and consequently of transactions)

PHP, servlet - JSP, ASP, XSP (Cocoon) see Java and PHP for more details

Page 38: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

38Web and DB - INT Evry

Java and Web

JVM concept is well adapted for Web Java language is widely used JDBC (standard API for DBMSs) Applets servlets Java Server Pages

Page 39: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

39Web and DB - INT Evry

Java - Java and Web

language for W3 client» applet = small compiled Java application

– Loaded from a W3 server

– Running is secured by the client JVM

language for W3 server » servlet = compiled Java application

– Stored on server-side

– Run by server-side JVM

Page 40: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

40Web and DB - INT Evry

Servlets

Servlet 2.2 (http://java.sun.com/servlet) Tomcat from Apache is the reference

implementation Same as CGI for Java CGI extensions (session, buffer

management, redirection/chaining)

Page 41: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

41Web and DB - INT Evry

Servlet Architecture

WebbrowserClient Servlet container

servlet2servlet1

HTTPServer

Container manages servlets (activation, desactivation)

Page 42: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

42Web and DB - INT Evry

Servlet object Model 

request Object servlet1 Response Object

session Object

getParametergetAttributegetHeadersgetCookies

setBufferSizesetHeadersetRedirect

getAttribute

Page 43: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

43Web and DB - INT Evry

JSP JSP 1.2 (http://java.sun.com/jsp) Tomcat from Apache is the reference

implementation Scripts included into HTML pages Same as ASP, PHP but for Java compiled into servlets portability of Java Server-side scripting with extensibility using tag

libraries

Page 44: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

44Web and DB - INT Evry

Example of JSP page<html><%@ page language=« java » import=« java.util.* » %><h1>Welcome</h1><p>Date: <p><jsp:usebean id=« clock » class=« jspCalendar »/><ul><li>Day : <%= clock.getDayOfMonth() %><li>Year : <%= clock.getYear() %></ul><%-- Test for AM or PM --%><%! Int time = Calendar.getInstance().get(Calendar.AM_PM); %><% if (time == Calendar.AM) { %>Good Morning<% } else { %>Good Afternoon<% } %><@ include file=« copyright.html » %></html>

Page 45: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

45Web and DB - INT Evry

JSP page Components (1)

JSP actions (or tags) : allow to call beans (XML syntax)

directives : processed by JSP engine during page compilation into servlets (include, import, ...)

declarations : as in Java

Page 46: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

46Web and DB - INT Evry

JSP page Components (2)

expressions : variables or constants integrated into the HTML result page

scriplets : blocks of Java code integrated into a JSP page

comments

Page 47: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

47Web and DB - INT Evry

Personalized Tags

One can add his/her own tags to simplify JSP page writing

tags libraries mechanism to define new tags is

complex

Page 48: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

48Web and DB - INT Evry

Example use of tag libraries

<html><%@ taglib uri=« http://acme.com/taglibs/simpleDB.tld »prefix=« x » %><x:queryBlock connData=« defude/bruno@MICA »> <x:queryStatement>select number, sname from students

</x:queryStatement> The first 10 students are: <table> <tr><th>Number</th><th>Name</th> <x:queryCreateRows from =« 1 » to=« 10 »>

<td><x:queryDisplay field=« number »/></td><td><x:queryDisplay field=« sname »/></td>

</x:queryCreateRows> </table></x:queryBlock></html>

Page 49: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

49Web and DB - INT Evry

Example (cont)

Definition of a queryBlock tag composed itself by other tags (queryStatement, queryCreateRows)

Allow to mask JDBC code to the page developer

a tag is defined by a descriptor (XML document) + a Java class for implementation

Page 50: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

50Web and DB - INT Evry

XML and Web

XML offers a clear splitting between logical structure and presentation

Allow to develop software producing different format outputs of the same XML source (HTML vs PDF, WML vs HTML,...)

Page 51: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

51Web and DB - INT Evry

Publishing Environment for XML

Software environment allowing to produce different output formats from the same XML source

Cocoon from XML-Apache Often based on Java implementation

(Servlet, JSP)

Page 52: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

52Web and DB - INT Evry

Cocoon

Web browseror PDA Client

Doc.XML

XSLT Interpreter

FOP InterpreterPDF Doc.

HTML Doc.

WML Doc.

Cocoon

1 2

4

4

56

Apache+ Tomcat

3

Page 53: 1 Web and DB - INT Evry Web and databases Bruno DEFUDE Computer Science Dept

53Web and DB - INT Evry

Conclusion

Numerous software products and several solutions

choice depends on the objective:» Query web site: simple gateways of server-side

scripting language PHP, ASP» Different web sites produced from the same

source: XML» Corporate portals: JSP or Cocoon