Upload
randolf-bond
View
214
Download
0
Embed Size (px)
Citation preview
1Web and DB - INT Evry
Web and databases
Bruno DEFUDEComputer 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
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
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
5Web and DB - INT Evry
First solution: CGI scripts
Browser (client)
HTTPserver
DBMS
url
Html result
CGI
querystring
SQLHTML
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
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
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
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)
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
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
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)
13Web and DB - INT Evry
Coupling Web and DBMS
Gateways Principles Java approach (applets, servlets, JSP) Server-side scripting (PHP)
14Web and DB - INT Evry
Gateways Principles
Single-request gateways Multiple-request gateways
(transactional)
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
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)
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 ,…)
18Web and DB - INT Evry
DB access (simple html)
Browser (client)
HTTPserver
DBMS
url
Html result
CGI
Gateway
querystring
SQL data
HTML
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>
20Web and DB - INT Evry
DB access (client-side script)
Browser (client)
HTTPserver
DBMS
url
Html result
gatewayquerystring
SQL data
HTML
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>
22Web and DB - INT Evry
DB access (without http)
Browser (client)+ java, TCL program (applet, tclet …)
DBMS
Specificprotocol
Specific protocol: JDBC, IIOP (CORBA)
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
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
25Web and DB - INT Evry
Efficiency
CGI vs NSAPI, ISAPI (but proprietary solutions)
Decrease process number (if large number of clients) : multithreaded gateway
26Web and DB - INT Evry
Transactions
transaction = DB program (read and write sequence)
ACID properties– A : Atomicity– C : Consistency– I : Isolation– D : Durability
27Web and DB - INT Evry
Examples of transactional services
on-line ordering (music, books, planes, …)
bank insurance e-business!!!!
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, ...)
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
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)
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)
32Web and DB - INT Evry
transactional gateway
browser (Client) http Server cgi daemon gateway DBMS
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)
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
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
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)
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
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
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
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)
41Web and DB - INT Evry
Servlet Architecture
WebbrowserClient Servlet container
servlet2servlet1
HTTPServer
Container manages servlets (activation, desactivation)
42Web and DB - INT Evry
Servlet object Model
request Object servlet1 Response Object
session Object
getParametergetAttributegetHeadersgetCookies
setBufferSizesetHeadersetRedirect
getAttribute
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
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>
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
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
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
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>
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
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,...)
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)
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
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