SQL: Queries, Programming, Triggers by 8bveTdr

VIEWS: 8 PAGES: 127

									                                 Chapter 7


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   1
     Lecture Overview
        Internet Concepts
        Web data formats
            HTML, XML, DTDs
        Introduction to three-tier architectures
        The presentation layer
            HTML forms; HTTP Get and POST, URL encoding;
             Javascript; Stylesheets. XSLT
        The middle tier
            CGI, application servers, Servlets, JavaServerPages,
             passing arguments, maintaining state (cookies)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      2
     Uniform Resource Identifiers
        Uniform naming schema to identify resources on
         the Internet
        A resource can be anything:
            Index.html
            mysong.mp3
            picture.jpg
        Example URIs:
                    http://www.cs.wisc.edu/~dbbook/index.html
                         mailto:webmaster@bookstore.com

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   3
     Structure of URIs
           Name of                                         Name of the
       protocol used to                                     resource
       access resource

   http://www.cs.ucf.edu/~kienhua/classes/COP4710/Ch7_internetapp.ppt



          Name of host                 URLs are a subset of URIs
           computer                           URL (Universal Resource Locator)
                                              The distinction is not important
                                               for our purposes

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                    4
     Hypertext Transfer Protocol (HTTP)
        What is a communication protocol?
               Set of standards that defines the structure of messages
               Examples: TCP, IP, HTTP


        What happens if you click on
                        www.cs.ucf.edu/~kienhua/classes/ ?
          1. Client (web browser) sends HTTP request to server
          2. Server replies with an HTTP response



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            5
     HTTP Requests
     HTTP Requests consists of several lines of ASCII text,
     with an empty line at the end.
            HTTP               URI              HTTP
           Method             field            version

                                                                 The type of the client
              GET ~/index.html HTTP/1.1        (e.g., versions of Netscape
                                                  or Internet Explorer)
              User-agent: Mozilla/4.0
              Accept: text/html, image/gif, image/jpeg

          The type of files the client is willing to
          accept (e.g., this client cannot accept
                      an mpg video)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                            6
     HTTP Responses
         The server retrieves the page index.html and
          uses it to assemble the HTTP response message
         The HTTP response message has three parts:
             status line
             several header lines
             body of the message (which contains the requested
              object)




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    7
     HTTP Response: Status Line
                             HTTP             Status             Associated
                            version            code                server
                                                                  message

                                   HTTP/1.1 200 OK

     Common status codes and associated messages:
        200 OK: The request succeeded and the object is in the
         body of the message
        400 Bad Request: The request could not be fulfilled
        404 Not Found: The requested object does not exist
        505 HTTP Version Not Supported: The protocol version
         used by the client is not supported by the server
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                8
     HTTP Responses: Header Lines
                                                                   Server time



                       Date: Mon, 04 Mar 2002 12:00:00 GMT
     Server type       Server: Apache/1.3.0 (Linux)
                       Last-Modified: Mon, 01 Mar 2002 09:23:24 GMT
     Number of
       bytes           Content-Length: 1024
     being sent        Content-Type: text/html               Date when
                                                                         the object
                                                                        was created
                                                  Type of object
                                                    being sent



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                        9
     HTTP Response: Body

        <HTML> <HEAD></HEAD>
        <BODY>
        <h1>Barns and Nobble Internet Bookstore</h1>
        Our inventory:
        <h3>Science</h3>
        <b>The Character of Physical Law</b>
        ...



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   10
     HTTP is Stateless
       HTTP is stateless
            No “sessions”
            Every message is completely self-contained
            No previous interaction is “remembered” by the protocol
            Tradeoff between ease of implementation and ease of application
             development: Other functionality has to be built on top

       Implications for applications:
          Any state information (shopping carts, user login-information) need
           to be encoded in every HTTP request and response!
          Popular methods on how to maintain state:
              • Cookies (later this lecture)
              • Dynamically generate unique URL’s at the server level (later this lecture)


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                               11
     Web Data Formats
      HTML:          The presentation language for the Internet
      Xml:           A self-describing, hierarchal data model
      DTD:           Standardizing schemas for Xml
      XSLT:          not covered in this class




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke     12
     HTML: Basic Contructs
     <html>




                               An HTML document
                               is enclosed by these
                               two tags



                                           Commands in HTML consist of
                                           a start tag and an end tag
     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           13
     HTML: Basic Contructs
     <html>
     <head>
     …                          The head section contains
                                information about the
     </head>                    page including the title,
                                author, etc.




     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   14
     HTML: Basic Contructs
     <html>
     <head>
     …
     </head>
     <body>
                                The body section contains
                                the parts of the web page
                                the browser will display:
                                text, images, links, etc.

     </body>
     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   15
     HTML: Basic Contructs
     <html>
     <head>
     …
     </head>                                      There are six levels
     <body>                                       of section headers:
     <h1>Section 1</h1>                           h1 through h6




     </body>
     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           16
     HTML: Basic Contructs
     <html>              • Ordered List
     <head>                   <ol>                 • Definition List
     …                           <li>Coffee</li>        <dl>
                                 <li>Tee</li>             <dt>Coffee</dt>
     </head>                  </ol>                       <dd> … </dd>
     <body>                                               <dt>Tee</dt>
     <h1>Section 1</h1>                                   <dd> … </dd>
      <ul>                                              </dl>
        <li>This is the first item</li>
      </ul>                                      This is an
     </body>                                     unordered list
     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke              17
     HTML: Basic Contructs
     <html>
     <head>
     …
     </head>
     <body>                          Display “first”
     <h1>Section 1</h1>              in boldface
      <ul>
        <li> This is the <b>first</b> item <\li>
      </ul>
     </body>
     </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   18
     HTML: An Example

<html>
  <head></head>                                <h3>Fiction</h3>
  <body>                                       <b>Oliver Twist</b>
  <h1>Barns and Nobble Internet                 <ul>
   Bookstore</h1>
                                                 <li>Author: Charles Dickens</li>
   <h3>Science</h3>                              <li>Published 2002</li>
                                                </ul>
   <b>The Character of Physical Law</b>
     <ul>                                      <b>Pride and Prejudice</b>
      <li>Author: Richard Feynman</li>          <ul>
      <li>Published 1980</li>                    <li>Author: Jane Austen</li>
      <li>Hardcover</li>                         <li>Published 1983</li>
     </ul>                                       <li>Paperback</li>
                                                </ul>
                                              </body>
                                             </html>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                      19
     HTML: Summary
        HTML is a markup language
        Commands are tags:
            Start tag and end tag
            Examples:
                • <HTML> … </HTML>
                • <UL> … </UL>

        Many editors automatically generate HTML directly
         from your document (e.g., Microsoft Word has an
         “Save as Web Page” facility)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   20
     HTML vs XML
      HTML
         Supports a fixed set of tags
         Not enough tags to describe the structures of the content
          of more general applications
                                                        <b>The Character of Physical Law</b>
                                                            <ul>
                                  What is the                <li>Author: Richard Feynman</li>
                                  first name ?                <li>Published 1980</li>
                                                              <li>Hardcover</li>
      XML                                                  </ul>

         Allows users to define new tags to structure any type of
          data or document
         It makes database systems more tightly integrated into
          Web applications
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                  21
     XML – Extensible Markup Language

         Language
            A way of communicating information

         Markup
            Notes or meta-data that describe your data or language

         Extensible
            Limitless ability to define new languages or data sets




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke        22
     XML Elements
      Elements are also called tags
      Elements are primary building blocks of an XML
       document
      Each element of a user-defined type ELM is
       enclosed by <ELM> and </ELM>
                                                                 Defined
                                                                  type

             Example: <FIRSTNAME>Jessica</FIRSTNAME>

                                             Data


        XML elements are case sensitive: BOOK ≠ Book

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke             23
     XML Nested Elements

      Elements can be nested (forming a tree structure)
               Example: <BOOK>
                                <AUTHOR>
                                  <FIRSTNAME>Charles</FIRSTNAME>
                                  <LASTNAME>Dickens</LASTNAME>
                                <AUTHOR>
                              </BOOK>




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke     24
     XML Elements /w Attributes
        An Element can have descriptive attributes
        The values of attributes are set inside the start tag
         of the element
        All attribute values must be enclosed in quotes

           Example: <BOOK GENRE=“Fiction” FORMAT=“Hardcover”>
                              <AUTHOR>
                                 <FIRSTNAME>Charles</FIRSTNAME>
                                 <LASTNAME>Dickens</LASTNAME>
                              <AUTHOR>
                           </BOOK>

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    25
     XML – Structure
        Xml looks like HTML
        Xml is a hierarchy of user-defined tags called
         elements with attributes and data
        Data are described by elements, elements are
         described by attributes

         <BOOK genre="Science" format="Hardcover">…</BOOK>
                      attribute
          open tag                                               data closing tag
        element name           attribute value



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                      26
     XML Entity References
        Whenever an entity reference appears in the
         document, it is textually replaced by its content
        Format: &lt; “lt” is an entity reference for the
         character “<“
        XML Data must not contain the reserved characters
             Reserved Characters       Entity References
                      <                       lt                 &apos;1&lt;5&apos;
                      >                       gt
                      &                      amp
                      “                      quot
                      ‘                      apos                       ‘1 < 5’

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                        27
     XML: Comments
      Comments start with <!- and end with ->
      Comments can contain arbitrary text except the
       string --
      Example: <!- comment ->




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   28
                                                               References no

     XML: An Example                                          external entities


                  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      If used it must be
                  <BOOKLIST>                                                       the first line
                    <BOOK genre="Science" format="Hardcover">
                      <AUTHOR>
      A root            <FIRSTNAME>Richard</FIRSTNAME><LASTNAME>Feynman</LASTNAME>
    element           </AUTHOR>
   contains all       <TITLE>The Character of Physical Law</TITLE>
      other           <PUBLISHED>1980</PUBLISHED>
    elements        </BOOK>
                    <BOOK genre="Fiction">
                      <AUTHOR>
                        <FIRSTNAME>Charles</FIRSTNAME><LASTNAME>Dickens</LASTNAME>
                      </AUTHOR>
                      <TITLE>Oliver Twist</TITLE>
                      <PUBLISHED>2002</PUBLISHED>                       All elements must
                    </BOOK>                                            be properly nested
                    <BOOK genre="Fiction">
                      <AUTHOR>
                        <FIRSTNAME>Jane</FIRSTNAME><LASTNAME>Austen</LASTNAME>
                      </AUTHOR>
                      <TITLE>Pride and Prejudice</TITLE>
                      <PUBLISHED>1983</PUBLISHED>
                    </BOOK>
                  </BOOKLIST>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                      29
     XML – What’s The Point?
        You can include your data and a description of
         what the data represents
            This is useful for defining your own language or protocol
         Example: Chemical Markup Language
                <molecule>
                      <weight>234.5</weight>
                      <Spectra>…</Spectra>
                      <Figures>…</Figures>
                </molecule>


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           30
     Xml – Storage
      Storage is done just like an n-ary tree
            <root>                                               Type: Element_Node
                                                          Node   Name: Element
               <tag1>                                            Value: Root
                  Some Text
                                                                 Type: Element_Node
                  <tag2>More</tag2>
                                                          Node   Name: Element
               </tag1>                                           Value: tag1

            </root>
                               Type: Text_Node                          Type: Element_Node
                               Name: Text          Node          Node   Name: Element
                               Value: Some Text                         Value: tag2


                                                                        Type: Text_Node
                                                                 Node   Name: Text
                                                                        Value: More

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                               31
      HTML vs XML: Review
     HTML                                           <b>The Character of Physical Law</b>
                                                         <ul>
        Supports a fixed set of tags                     <li>Author: Richard Feynman</li>
        Not enough tags to                               <li>Published 1980</li>
         describe the structures of                       <li>Hardcover</li>
                                                         </ul>                    What is the
         the content of more                                                      first name ?
         general applications
                                                                  Useful for
                                                                 defining your
     XML                                                        own language

        Allows users to define new                   <BOOK>
         tags (elements) to                               <AUTHOR>
                                                            <FIRSTNAME>Charles</FIRSTNAME>
         structure any type of data                         <LASTNAME>Dickens</LASTNAME>
         or document                                      <AUTHOR>
                                                      </BOOK>

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                   32
     XML vs. HTML
       HTML describes presentation
          An HTML document rendered in a Web browser is
           human readable

       XML describes content
          It provide the message syntax for request-response
           message exchange between applications
          It facilitate loosely coupled heterogeneous
           applications

            HTML is for machine-to-person communication,
            and XML is for machine-to-machine communication.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   33
     DTD – Document Type Definition
      Unlike HTML, XML allows user-defined elements (tags)
        → the user needs to describe these elements

      DTD is a set of rules that defines the user-defined
       elements for an XML document
        → DTD is the schema for the XML data
        → DTD says what elements and attributes are required or
          optional (the formal structure of the language)

      A document is valid if it is structured according to the
       rules set by the DTD
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    34
        DTD Structure
   <!DOCTYPE BOOKLIST [




                                      A DTD is enclosed in:
                                      <!DOCTYPE name [DTDdeclaration]>




   ]>


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           35
        DTD Structure
   <!DOCTYPE BOOKLIST [
   <!ELEMENT BOOKLIST (BOOK)*>




                             • A DTD starts with the root element
                             • The root element BOOKLIST consists
                               of zero or more BOOK elements
                                  * : zero or more occurrences
                                  + : one or more occurrences
                                  ? : zero or one occurrence
   ]>


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      36
        DTD Structure
   <!DOCTYPE BOOKLIST [
   <!ELEMENT BOOKLIST (BOOK)*>
      <!ELEMENT BOOK (AUTHOR,TITLE,PUBLISHED?)>



               • An element can have nested elements
               • This rule says that a BOOK element
                 contains an AUTHOR element, a TITLE
                 element, and an optional PUBLISHED
                 element
   ]>


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   37
        DTD Structure
   <!DOCTYPE BOOKLIST [
   <!ELEMENT BOOKLIST (BOOK)*>
      <!ELEMENT BOOK (AUTHOR,TITLE,PUBLISHED?)>
           <!ELEMENT AUTHOR (FIRSTNAME,LASTNAME)>
                 <!ELEMENT FIRSTNAME (#PCDATA)>
                 <!ELEMENT LASTNAME (#PCDATA)>


              • Instead of containing other elements, an
                element can contain actual text
                    #PCDATA indicates character data
   ]>               EMPTY indicates the element has no content
                    ANY indicates that any content is permitted.
                     No checking inside this structure (avoided
                     whenever possible)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      38
        DTD Structure
                      • Attributes of elements are declared
   <!DOCTYPE BOOKLIST [ outside the element
                      (BOOK)*>
   <!ELEMENT BOOKLIST • The BOOK element has two attributes
        <!ELEMENT BOOK (AUTHOR,TITLE,PUBLISHED?)>
                              The GENRE attribute is required and can have
                               the value ‘Science’ or ‘Fiction’
             <!ELEMENT AUTHOR (FIRSTNAME,LASTNAME)>
                              The FORMAT attribute can have the value
                               FIRSTNAME (#PCDATA)>
                    <!ELEMENT ‘Paperback’ or ‘Hardcover’, and ‘Paperback’ is
                    <!ELEMENT the default value
                               LASTNAME (#PCDATA)>
                              #REQUIRED
             <!ELEMENT TITLE (#PCDATA)> is the default option
             <!ELEMENT PUBLISHED (#PCDATA)>
        <!ATTLIST BOOK GENRE (Science|Fiction) #REQUIRED>
        <!ATTLIST BOOK FORMAT (Paperback|Hardcover) “Paperback”>
   ]>
                                                Attribute not required.
                                                Default value is “Paperback”
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                 39
     The whole DTD
   <!DOCTYPE BOOKLIST [
   <!ELEMENT BOOKLIST (BOOK)*>
      <!ELEMENT BOOK (AUTHOR,TITLE,PUBLISHED?)>
           <!ELEMENT AUTHOR (FIRSTNAME,LASTNAME)>
                  <!ELEMENT FIRSTNAME (#PCDATA)>
                  <!ELEMENT LASTNAME (#PCDATA)>
           <!ELEMENT TITLE (#PCDATA)>
           <!ELEMENT PUBLISHED (#PCDATA)>
      <!ATTLIST BOOK GENRE (Science|Fiction) #REQUIRED>
      <!ATTLIST BOOK FORMAT (Paperback|Hardcover) “Paperback”>
   ]>


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   40
     Five Possible Content Types
                <!ELEMENT element-name (contentType)>
      Other elements
      Special symbol #PCDATA, EMPTY, or ANY
      A regular expression constructed from the preceding
       four choices
             exp1, exp2, exp3: An ordered list of regular expressions
             Exp*: An optional expression (zero or more occurrence)
             Exp?: An optional expression (zero or one occurrences)
             Exp+: A mandatory expression (one or more occurrences)
             Exp1 | exp2: exp1 or exp2

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           41
     PCDATA vs. CDATA

         PCDATA:              Parsed Character Data

         CDATA:              (Unparsed) Character Data

         PCDATA    and CDATA are used to assert
            something about the allowable content
            of elements and attributes, respectively.



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   42
     PCDATA
  PCDATA: Parsed Character Data
        Character data that is to be parsed by the XML
         parser. It checks for entity references (e.g., “&lt;” ,
         “&amp;”) and replace them.
        Example: <!ELEMENT FORMULA (#PCDATA)>
                 Note: “<“ is illegal in XML elements because
                 the parser interprets it as the start of a new
                 element



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke     43
     CDATA

        CDATA               section
              Not to be parsed by the XML
               parser

        CDATA-type attribute value

              Parsed by the XML parser


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   44
      CDATA Section
     A CDATA section can be used within an element content, that is
      marked for the XML parser to interpret as only character data,
      not markup.
            <program>
            <![CDATA[
            function smaller(a,b)             • A CDATA section
            {
            if (a > 0 && a < b) then          • Everything inside the
              {
              return a;                         CDATA section is ignored
              }                                 by the XML parser
            else
              {
              return b;
              }
            }
            ]]>
            </program>

     CDATA sections are useful for including code as text data within
      an XML document
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke             45
      CDATA-type attribute value
     An attribute value may be designated as being of
      type CDATA
     Character and entity reference markup is allowed
      and will be processed by the XML parser

                                                                 The attribute is
                                                                  not required


     Example: <!ATTLIST math expr CDATA #IMPLIED>
        A valid element: <math expr =“1 + 2 &lt; 5” />
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                      46
     DTD – An Example
     <?xml version='1.0'?>
     <!ELEMENT Basket (Cherry+, (Apple | Orange)*) >                    Element has
                                                                        no content
        <!ELEMENT Cherry EMPTY>
            <!ATTLIST Cherry flavor CDATA #REQUIRED>
        <!ELEMENT Apple EMPTY>
            <!ATTLIST Apple color CDATA #REQUIRED>                      Apple’s color
                                                                         is required.
        <!ELEMENT Orange EMPTY>                                         Cherry should
            <!ATTLIST Orange location ‘Florida’>                            go first.


         <Basket>                                    <Basket>
            <Cherry flavor=‘good’/>                     <Apple/>
            <Apple color=‘red’/>                        <Cherry flavor=‘good’/>
            <Apple color=‘green’/>                      <Orange/>
         </Basket>                                   </Basket>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                          47
     DTD – Well-Formed and Valid

     Well-Formed: the document conforms to the syntax
     rules of XML
            Has both start tags and end tags
            Elements do not overlap

     Valid XML: the document is well-formed and
     conforms to a DTD (schema)



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   48
     Well-Formed and Valid Example
            <?xml version='1.0'?>                         DTD
            <!ELEMENT Basket (Cherry+)>
               <!ELEMENT Cherry EMPTY>
                   <!ATTLIST Cherry flavor CDATA #REQUIRED>

       Not Well-Formed        Well-Formed but Invalid
       <basket>               <Job>
         <Cherry flavor=good>   <Location>Home</Location>
       </Basket>              </Job>

                           Well-Formed and Valid
                           <Basket>
                             <Cherry flavor=‘good’/>
                           </Basket>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   49
     Wrapping
         If the DTD is to be included in the XML source file, it
         should be wrapped in a DOCTYPE definition

            <?xml version='1.0'?>
            <!DOCTYPE Basket [
            <!ELEMENT Basket (Cherry+)>
  DTD              <!ELEMENT Cherry EMPTY>
                          <!ATTLIST Cherry flavor CDATA #REQUIRED>
            ]>
            <Basket>
               <Cherry flavor=‘good’/>
            </Basket>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke       50
     XML and DTDs
       More and more standardized (domain-specific) DTDs
        will be developed
          MathML (Mathematical Markup Language)
          Chemical Markup Language

       Enable seamless data exchange among
        heterogeneous sources

       Sophisticated query languages for XML are available:
          Xquery
          XPath
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   51
     Lecture Overview
        Internet Concepts
        Web data formats
            HTML, XML, DTDs
        Introduction to three-tier architectures
        The presentation layer
            HTML forms; HTTP Get and POST, URL encoding;
             Javascript; Stylesheets. XSLT
        The middle tier
            CGI, application servers, Servlets, JavaServerPages,
             passing arguments, maintaining state (cookies)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      52
     Components of Data-Intensive
     Systems
      Three separate                  types of functionality:
              Data management
              Application logic
              Presentation

      The    system architecture determines whether
         these three components reside on a single
         system (tier) or are distributed across several
         tiers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   53
     Single-Tier Architectures
      All functionality combined into a single tier, usually
      on a mainframe
             User access through dumb terminals

      Advantages:
             Easy maintenance and administration
      Disadvantages:
             Today, users expect graphical user interfaces.
             Centralized computation of all of them is too much for
              a central system

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke         54
     Client-Server Architectures
     Work division: Thin client
                                                            Application             Client
        Client implements only the




                                                                          Network
                                                              Logic
                                                                                       .
                                                                                       .
         graphical user interface                                                      .
        Server implements business                         Database
                                                                                    Client
         logic and data management

        Work division: Thick client                                                  Client
        Client implements both the                                                  Application




                                                                          Network
                                                                                       Logic

         graphical user interface and                                                      .
                                                                                           .
                                                                                           .
         the business logic                                 Database                  Client
                                                                                     Application
        Server implements data                                                        Logic


         management
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                     55
     Disadvantages of Thick Clients
      No central place to update the business logic
      Security issues: Server needs to trust clients
         Clients need to leave server database in consistent state
         One possibility: Encapsulate all database access into
          stored procedures
      Does not scale to more than several 100s of clients
        Large data transfer between
                                                                                  Client
         server and client                                                       Application




                                                                       Network
                                                                                   Logic
        More than one server creates                                                 .
                                                                                      .
                                                                                      .
         a problem: x clients, y servers:                   Database              Client
                                                                                 Application
         x*y connections                                                           Logic


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                 56
     Three-Tier Architecture

            Data management                    Middle                      Presentation
                  tier                          tier                           tier

                                                                                Client
                                    Network




                                                                 Network
                                                                             (Web Browser)
                                              Application                      .
                                                                               .
                   Database                     Logic                          .
                                                                                Client
                                                                             (Web Browser)




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                               57
     The Three Layers
     1.   Presentation tier
            Primary interface to the user
            Needs to adapt to different display devices (PC, PDA, cell
             phone, voice access?)
     2.   Middle tier
            Implements business logic (implements complex actions,
             maintains state between different steps of a workflow)
            Accesses different data management systems
     3.   Data management tier
            One or more standard database management systems

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            58
     Example 1: Airline reservations
        Database System
            Airline info, available seats, customer info, etc.

        Application Server
            Logic to make reservations, cancel reservations, add
             new airlines, etc.

        Client Program
            Log in different users, display forms and human-
             readable output



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      59
     Example 2: Course Enrollment
       Database System
          Student info, course info, instructor info, course
           availability, pre-requisites, etc.
       Application Server
          Logic to add a course, drop a course, create a new course,
           etc.
       Client Program
          Log in different users (students, staff, faculty), display
           forms and human-readable output



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke          60
     Technologies

                                                                 HTML
                              Client Program
                                                                 Javascript
                            (Web Browser)                        XSLT

                                                                 JSP
                           Application Server
                                                                 Servlets
                      (e.g., Tomcat, Apache)                     Cookies
                                                                 CGI

                            Database System                      XML
                         (e.g., mySQL, DB2)                      Stored Procedures

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                       61
     Advantages of the Three-Tier
     Architecture
        Heterogeneous systems
            Tiers can be independently maintained, modified, and replaced

        Thin clients
            Clients only need enough computation power for the presentation layer
             (web browsers)

        Integrated data access
            Several database systems can be handled transparently at the middle tier
            Central management of connections

        Scalability
            Replication at middle tier permits scalability of business logic

        Software development
            Code for business logic is centralized
            Interaction between tiers through well-defined APIs: Can reuse standard
             components at each tier
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                          62
     Lecture Overview
        Internet Concepts
        Web data formats
            HTML, XML, DTDs
        Introduction to three-tier architectures
        The presentation layer
            HTML forms; HTTP Get and POST, URL encoding;
             Javascript; Stylesheets. XSLT
        The middle tier
            CGI, application servers, Servlets, JavaServerPages,
             passing arguments, maintaining state (cookies)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      63
     Overview of the Presentation Tier
        Recall: Functionality of the presentation tier
            Primary interface to the user
            Needs to adapt to different display devices (PC, PDA, cell
             phone, voice access?)
            Simple functionality, such as field validity checking

        We will cover:
            HTML Forms: How to pass data to the middle tier
            JavaScript: Simple functionality at the presentation tier
             (e.g., simple animations)
            Style sheets: Present the same webpage with different
             formatting for clients with different capabilities.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            64
     HTML Forms
     Common way to communicate data from client to
      middle tier
     General format of a form:

            <FORM ACTION=“page.jsp” METHOD=“GET”
                  NAME=“LoginForm”>
            …
            </FORM>

       Inside an HTML form,
          We have form elements that allow the user to enter
           information in a form
          We can use any HTML tags
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   65
     Attributes of Form Tag
              <FORM ACTION=“page.jsp” METHOD=“GET”
                       NAME=“LoginForm”>
              …
              </FORM>
       ACTION: Specifies URI of the page to which the form contents
        are submitted (if absent, URI of current page is used)
          → the page provides logic for processing input from the form.
       METHOD: Specifies HTTP GET or POST method for form
        submission (more on this later)
       NAME: Name of the form; can be used in client-side scripts to
        refer to the form (more on this later)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke          66
     Form Element - Example 1
      INPUT is the most used form tag:
        <FORM>
        First name:
        <INPUT TYPE=“text” NAME=“firstname”>
        <br>
        Last name:
        <INPUT TYPE=“text” NAME=“lastname”>
        </FORM>
                                                                 20 characters
      Display          in a browser:                             by default

                First name:
                Last name:
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                   67
     Form Element - Example 2
      Using          radio buttons:
           <FORM>
           <INPUT TYPE=“radio” NAME=“under”> Undergraduate
           <br>
           Last name:
           <INPUT TYPE=“radio” NAME=“grad”> Graduate
           </FORM>

      Display in              a browser:
                          Undergraduate
                          Graduate
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   68
        Form Element - Example 3
       Using Submit botton:
        <FORM ACTION=“phone.jsp” METHOD=“GET” NAME=“Phone”>
    1   Phone number:
    2   <INPUT TYPE=“text” NAME=“phonenum”>
    3   <INPUT TYPE=“submit” VALUE=“Submit”>
        </FORM>
       Display in a browser:
                                                             2            3
          1
            Phone number:                                        Submit

       When “Submit” button is clicked, the content of the
        form is sent to the page called phone.jsp on the server
       The page named action needs to be a program, script,
        or page that will process the user input
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                69
     INPUT - General Format
     <INPUT TYPE=“text” NAME=“username” VALUE=“Joe”>


    text: a text input field           • Specifies the symbolic    • Specifies default
                                         name for this field         value for text or
    password: a text field                                           password fields
    where the entered                  • Used to identify the
    characters are                       input field when it is    • For submit or
    displayed as stars                   sent to the middle tier     reset buttons, it
                                                                     determines the
    reset: a button that                                             label of the
    resets all input fields                                          button
    submit: a button that
    sends the values in the
    form to the server                  Name:                           Submit

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                           70
     Three Ways of Specifying User Input
    INPUTtag
    TEXTAREA tag
       <textarea name=“comments” cols=“45” row=“10”>
       Please give your input here – as many words as you like.
       </textarea>
       Please give your input here – as many words as you like
                                                                  SELECT    tag
                                                                   Allows the user to
                                                                   choose from a fixed
                                                                   set of values; one or
                                                                   several at once.




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                             71
     Passing Arguments
     Two methods for submitting HTML Form
     data to the Web server:
                 GET
                 POST


                    <FORM ACTION=“page.jsp” METHOD=“GET”
                           NAME=“LoginForm”>
                    …
                    </FORM>

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   72
     GET
                                      Before clinking on “Submit”
  The contents of the form
   are assembled into a
   query URI (i.e., Middle
   tier receives header that
   actually contains data
   from the form)                                                              Contents of
                                                                                the form
  The form contents are
   directly visible to the             After clinking on “Submit”
   user as the constructed
   URI

  The users can bookmark
                                                                 Constructed
   the page with the                                                URI
   constructed URI


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                           73
     POST
                                    Before clinking on “Submit”
  The contents of the
   form are encoded as in
   the GET method, but
   they are sent in a
   separate data block

  The form contents are
   sent inside the HTTP             After clinking on “Submit”
   request message body
   and are not visible to
   the user




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    74
     Example: Google Search


                                                       Using GET




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke     75
    Encoded URI                         <FORM ACTION=“page.jsp” METHOD=“GET”
                                        NAME=“loggin”>
                                        Login:
              Only shown for            <INPUT TYPE=“text” NAME=“username”> <br>
              GET method                <INPUT TYPE=“password” NAME=“password”> <br>
                                        <INPUTTYPE=“submit” VALUE=“Login”>
                                        </FORM>

     action?name1=value1&name2=value2&name3=value3

        The action is the URI                       ‘name=value’ pairs are
        specified in the ACTION                     the user inputs from the
        attribute of the FORM tag                   INPUT fields in the form

                                            ‘+’ represents a
                                            space character
     Example:
       page.jsp?username=John+Doe&password=secret
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                         76
    HTTP GET: Encoding Form Fields
          Form fields can contain general ASCII characters
           that cannot appear in an URI (e.g., space character)
                         Name:           Jane Doe

                                          Cannot have a space
                                          in an URI

          A special encoding convention converts such field
           values into “URI-compatible” characters:




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    77
    HTTP GET: Encoding Form Fields
          Form fields can contain general ASCII characters
           that cannot appear in an URI (e.g., space character)
          A special encoding convention converts such field
           values into “URI-compatible” characters:
          1. Convert all spaces to the “+” character
          2. Glue (name,value)-pairs from the form INPUT tags
              together with “&” to form the URI
         page.jsp?coursename=Database+Systems&CourseID=COP4710
          3. Convert all “special” characters to %xyz, were xyz is the
                                                      Second INPUT
             ASCII code of the First INPUT Special characters include
                               character.
             &, =, +, %, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke           78
     JavaScript
        JavaScript is an interpreted scripting language
            A JavaScript is a program added to a Web page.
            It runs at the client tier to add functionality to the
             presentation
        Sample applications:
            Browser Detection: Detect browser type and load
             browser-specific page.
            Form validation: Validate form input fields (e.g., an email
             address contains ‘@’), or if all required fields have input
             data.
            Browser control: Open new windows (e.g., pop-up ads)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke             79
     JavaScript: SCRIPT Tags
      A JavaScript is usually embedded inside the HTML
       with the <SCRIPT> … </SCRIPT> tag.
      <SCRIPT> tag has several attributes:
         LANGUAGE: indicates language of the script (such as
          JavaScript)
         SRC: Specifies the external file with the script code
      Example:
        <SCRIPT LANGUAGE=“JavaScript” SRC=“validate.js”>
        </SCRIPT>

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    80
     JavaScript without SRC Attribute
    If <SCRIPT> tag does not have an SRC attribute, then the
     JavaScript is directly in the HTML file.
                       Example: Create a pop-up box with a message
                                <SCRIPT LANGUAGE=“JavaScript”>
                                <!-- alert(“This is a pop-up alert example”)
                                //-->
                                                   JavaScript
                                </SCRIPT>          processor     HTML processor
                                                           ignores “- ->”     ignores this
    Two different commenting styles                                        JavaScript code
       <!-- marks the start of an HTML comment
       <!-- indicates the following JavaScript code should be ignored by the HTML
        processor
       // marks the start of a JavaScript comment
       // comment is used to comment out the HTML “-->” comment as it is
        interpreted otherwise
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                81
     Two Different Commenting Styles

           “//…” is used for single-line comments
           “/* … */ ” is used for multi-line comments




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   82
     JavaScript
         Variables:
             • numbers, Boolean values, strings, …
             • Variables do not have a fixed type, but implicitly have the
               type of the data to which they have been assigned
         Assignments: =, +=, …
         Comparison operators: <,>,…
         Boolean operators: && for logical AND, || for logical OR, !
          for negation
         Statements
             • if (condition) {statements;} else {statements;}
             • Loops: for-loop, do-while, and while-loop
         Functions with return values
                        function funcname(arg1, …, argk) {statements;}
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke               83
     A Complete Example
     <SCRIPT LANGUAGE=“JavaScript”>               “document” is an
                                                  implicitly defined
     <!--                                     variable referring to the
     function testLoginEmpty()                   current HTML page
     {
        loginForm = document.LoginForm
        if ((loginForm.userid.value == “”) ||              HTML Page
               (loginForm.password.value == “”)) {
        alert(‘Please enter userid and password.’); function testLoginEmpty
        return false;                                         …
                                                              … = document.LoginForm
        }                                                     …
                                        This slide
        else
               return true;                                  <FORM NAME=“LoginForm”
     }                                                       …
                                                             …
     //-->                             Next slide
                                                             </FORM>

     </SCRIPT>
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                         84
     A Complete Example – Cont’d
  <H1 ALIGN = “CENTER”>Barns and Nobble Internet Bookstore</H1>
  <H3 ALIGN = “CENTER”>Please enter your userid and password:</H3>
  <FORM NAME=“LoginForm” METHOD=“POST”                The form contents
                                                       are submitted to
          ACTION=“TableOfContents.jsp”                 server if function
          onSubmit=“return testLoginEmpty()”>         returns true (more
                                                         in next page)
 1 Userid: <INPUT TYPE=“TEXT” NAME=“userid”><P>
 2 Password: <INPUT TYPE=“PASSWORD” NAME=“password”><P>
 3 <INPUT TYPE=“SUBMIT” VALUE=“Login” NAME=“SUBMIT”>
 4 <INPUT TYPE=“RESET” VALUE=“Clear Input” NAME=“RESET”>
  </FORM>


                                                    1
                                                        2
                                    3           4

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke              85
     Event Handler
          <FORM NAME = “LoginForm” METHOD=“POST”
   Event handler,   ACTION=“TableOfContents.jsp”
   called if submit
       button is    onSubmit=“return testLoginEmpty()”>
       pressed

        An event handler is a function that is called if an
         event happens on an object in a webpage
        onSubmit is an event handler, which is called if the
         submit button is pressed
        If the event handler returns true, then the form
         contents are submitted to the server
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   86
     Stylesheets
       We need different ways of displaying the same
        information to clients with different displays
          Using different font sizes or colors to provide better
           contrast on a black-and-white screen
          Rearranging objects on the page to accommodate small
           screens
          Highlighting different information to focus on some
           important part of the page
       A style sheet is a method to adapt the same
        document contents to different presentation
        formats
          It tells a Web browser how to translate the data into a
           presentation that is suitable for the client’s display
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke       87
     Stylesheets
      Idea: (1) Separate display from contents, and (2) adapt
       display to different presentation formats
      Two aspects:
         Document transformations to decide what parts of the
          document to display in what order
         Document rendering to decide how each part of the
          document is displayed
      Two stylesheet languages
         Cascading style sheets (CSS): For HTML documents
         Extensible stylesheet language (XSL): For XML documents

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      88
     CSS: Cascading Style Sheets
        Styles are normally stored in style sheets, which are files
         that contain style definitions. They define how to display
         HTML documents.
        Many HTML documents (e.g., all in a website) can refer to
         the same CSS
            Can change format of a website by changing a single file (i.e.,
             separation of content from presentation)
        Example: Include the following line into an HTML file to link
         to the external CSS style sheet.
         <LINK REL=“style sheet” TYPE=“text/css” HREF=“books.css” />

            Relationship between             The type of the       Location of the
            current document and            linked document      linked document
               linked document

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                       89
      CSS: Example
  <LINK REL=“style sheet” TYPE=“text/css” HREF=“books.css”/>
                       The books.css file:
     Same effect as:             BODY {BACKGROUND-COLOR: yellow}
  <BODY BACKGROUND-
   COLOR=“YELLOW”>               H1 {FONT-SIZE: 36pt}
      Each line consists of
                                 H3 {COLOR: blue}
          three parts            P {MARGIN-LEFT: 50px; COLOR: red}

     Each line consists of three parts: selector {property: value}
        Selector: Tag whose format is defined
        Property: Tag’s attribute whose value is set
        Value:     value of the attribute
      Multiple properties for the same selector are separated by semicolons
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                 90
     XSL
        Language for expressing style sheets
            More at: http://www.w3.org/Style/XSL/

        Three components
            XSLT: XSL Transformation language
                • Can transform one document to another
                • More at http://www.w3.org/TR/xslt
            XPath: XML Path Language
                • Selects parts of an XML document
                • More at http://www.w3.org/TR/xpath
            XSL Formatting Objects
                • Formats the output of an XSL transformation
                • More at http://www.w3.org/TR/xsl/


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   91
     Lecture Overview
      Internet Concepts
      Web data formats
            HTML, XML, DTDs
      Introduction to three-tier architectures
      The presentation layer
            HTML forms; HTTP Get and POST, URL encoding;
             Javascript; Stylesheets. XSLT
        The middle tier
            CGI, application servers, Servlets, JavaServerPages,
             passing arguments, maintaining state (cookies)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      92
     Overview of the Middle Tier
                                                            Encodes
                                                            business
                                                              logic

                                FORM
                                                                        Database
                                                           CONNECTION
                              OUTPUT


    We will cover
       CGI: Protocol for passing arguments to programs running at the
        middle tier
       Application servers: Runtime environment at the middle tier
       Servlets: Java programs at the middle tier
       JavaServerPages: Java scripts at the middle tier
       Cookies: Used to maintain state at the middle tier.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                     93
     CGI: Common Gateway Interface

        Goal: Transmit arguments from HTML forms to
         application programs running at the middle tier

        Details of the actual CGI protocol unimportant
           libraries implement high-level interfaces (enable
          application programs to get arguments from the HTML
          form)




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   94
    CGI Illustration: Request the Page


          http:// …
                               Request the page (HTTP)
                                                                  Web
                                   Internet                      Server


                                                                          <FORM>
                                                                          …




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                     95
    CGI Illustration: Send Back the page



           <FORM>
           …            Fill the <FORM> and send back the page
                                                                  Web
                                   Internet                      Server


                                                                          <FORM>
                                                                          …




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                     96
    CGI Illustration: Process <FORM>
                                                        This is called a CGI script
                                                        since many such
                                                        programs are written in a
                                                        scripting language               XYZ.cgi
                                                                                          (child
                                                                                        Result
                                                                                        process)

                                              HTTP
           <FORM>                            protocol                  <FORM>
           …                                                           …
                                                                                          CGI
                                                                                 parameters
                                                                      Web               protocol
                                   Internet                          Server


                                                                                        <FORM>
                                                                                        …




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                     97
     CGI: Example
   HTML form:                  Program                               When user submits
                                                                     this page, values in the
   <form action=“findbooks.cgi” method=POST>                         input fields are passed
   Type an author name:                                              to the program
   <input type=“text” name=“authorName”>                             findbooks.cgi
   <input type=“submit” value=“Send it”>   An input
   <input type=“reset” value=“Clear form”>   field
   </form>
                                                            The header() function prints out the:
 Include the standard                 Perl code:            “Content-Type: text/html” statement.
 library CGI.pm                       use CGI;
 Instantiate a CGI object             $dataIn=new CGI;
                                      $dataIn->header();
 Extract the argument                 $authorName=$dataIn->param(‘authorName’);
 of the HTML form                     print(“<HTML><TITLE>Argument passing test</TITLE>”);
                                      print(“The author name is “, $authorName);
  Dynamically construct               print(“</HTML>”);
  the Webpage – result                                     Perl is often used for CGI
  of user request                     exit;
                                                                 scripting
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                      98
     CGI: Some Issues
        CGI applications run in a separate process, created
         at every invocation – not scalable.
            Remedy: FastCGI – using a single persistent process to
             handle many requests over its lifetime)

        No resource sharing between application programs
         (e.g., database connections)

        Remedy: Application servers (next few pages)


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke        99
     Application Servers
        The application program is invoked using the CGI
         protocol. Each page request results in the creation
         of a new process → Do not scale well to a large
         number of simultaneous requests.
        Avoid the process creation overhead:
            Application server maintains a pool of threads or
             processes and uses them to execute requests
            Application server also provides other functionality
                • Enable access to heterogeneous data sources (e.g., by providing
                  JDBC drivers.)
                • Provide APIs for session management

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                      100
     Application Server: Process Structure
                                                            The server directs the
                                                            request to be processed
                          HTTP Web Server                   by appropriate servlets
   Web Browser
                                     (e.g., Apache)
                                                                         C++ Application
    The client
    sends an
    HTTP                                                                 JavaBeans
    request to
    the server           Application Server                 JDBC
                                                                         DBMS 1
                                                           ODBC
                                                                         DBMS 2
   Java
   instead of                                                    The servlets do their processing,
   Perl as in             Pool of Servlets                       then return results to the client
   CGI                                                           normally in the form of HTML
                                                                 documents

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                   101
     What is a Servlet ?
       Servlets are Java’s answer to CGI programming
       Servlet is a Java class used to extend the capabilities
        of servers that host applications
       In most cases, servlets extend the specific
        HttpServlet class for Web servers that communicate
        with clients via HTTP
       HttpServlet class provides methods such as
          doGet (for HTTP GET) and doPost (for HTTP POST) to
           receive arguments from HTML forms, and
          sending output back to the client via HTTP
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    102
      Servlet Life Cycle
   Servlet container (e.g., Tomcat) is the intermediary
    between the Web server and the servlets in the
    container
   When a request arrives from the Web server:
        If an instance of the servlet does not exist, the container
            • Loads the servlet class
            • Creates an instance of the servlet class
            • Initializes the servlet instance (i.e., places it into service)
        Container calls service() method to allow the servlet to
         respond to the request. Two objects are passed:
            • The HttpServletRequest object contains the client’s HTTP request
              information, and
            • The HttpServletResponse encapsulates the servlet’s response
     Servlet container calls the destroy method before
      removing a servlet from service (e.g., to free memory)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                   103
     Review: HTTP Responses
      The HTTP response message has three parts:
             status line, e.g., “HTTP/1.1 200 OK” (The request
              succeeded and the object is in the body of the
              message.)
             several header lines
                           Date: Mon, 04 Mar 2002 12:00:00 GMT
                           Server: Apache/1.3.0 (Linux)
                           Last-Modified: Mon, 01 Mar 2002 09:23:24 GMT
                           Content-Length: 1024
                           Content-Type: text/html
             body of the message (which contains the requested
              object)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            104
Template of a Generic Servlet Structure
 This simple servlet just outputs two words “Hello World”
 import java.io.*;
                                                request object is         response object is
 import java.servlet.*;                         used to read              used to specify:
 import java.servlet.http.*;                    HTML form data            (1) the response
                                                                          status code, and
                                                                          (2) headers of the
                                                                          HTTP response
 public class ServletTemplate extends HttpServlet {
   public void doGet(HTTPServletRequest request,
                                   HTTPServletResponse response)
   throws ServletException, IOException {
         PrintWriter out=response.getWriter();
         out.println(“Hello World”); //Use ‘out’ to send content to browser
   }                  Use ‘out’ to compose     Returns a PrintWriter
 }                    content that is          object that can send
                                               character text to the
                             returned to the client
                                                                 client
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                             105
     Servlets: A Complete Example
                                                                       Used for extracting
       public class ReadUserName extends HttpServlet {                 data from the HTML
           public void doGet(           HttpServletRequest request,    form
                                        HttpSevletResponse response)
                  throws ServletException, IOException {
                  reponse.setContentType(“text/html”);              Used for generating
                  PrintWriter out = response.getWriter();             the output HTML
                  out.println(“<HTML><BODY>\n <UL> \n” +
                             “<LI>” + request.getParameter(“userid”) + “\n” +
                             “<LI>” + request.getParameter(“password”) + “\n” +
                             “<UL>\n<BODY></HTML>”);
           }
           public void doPost(         HttpServletRequest request,
                                       HttpSevletResponse response)
                  throws ServletException, IOException {
                  doGet(request,response);
           }
       }
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                               106
      Servlets: A Complete Example
        public class ReadUserName extends HttpServlet {
            public void doGet(          HttpServletRequest request,       Prepare
                                                                          “Content-Type: text/html”
                                        HttpSevletResponse response)      statement in the header
                  throws ServletException, IOException {                  of the Web page
 Use ‘out’ to     reponse.setContentType(“text/html”);
 compose          PrintWriter out = response.getWriter();       Extract data
 content that                                                    from form
                  out.println(“<HTML><BODY>\n <UL> \n” +
 is returned to
                             “<LI>” + request.getParameter(“userid”) + “\n” +
 the client
                             “<LI>” + request.getParameter(“password”) + “\n” +
                             “<UL>\n<BODY></HTML>”);
            }
            public void doPost(        HttpServletRequest request,       We extend the
                                       HttpSevletResponse response)      servlet to
                  throws ServletException, IOException {                 dynamically
                  doGet(request,response);                               generate HTML (by
            }                                                            writing to the
        }                                                                PrintWriter object)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                                        107
     JavaServer Pages (JSP)
        Servlets
            Generate HTML by writing it to the “PrintWriter” object
            Code first, webpage second

        JavaServer Pages
            Written in HTML, Servlet-like code embedded in the
             HTML
            Webpage first, code second
            They are usually compiled into a Servlet



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke         108
     JavaServer Pages
        Change the file extensions to “.jsp” instead of “.html”
        Embed Java expressions in JSP pages by putting them
         between <%= and %>                  Code embedded
                                                                 in HTML
             The time is now <%= date %>
        Embed block of Java code (called scriplet) between <% and
         %>
        There are a number of useful predefined objects for scriplet:
           The variable out can be used to generate HTML
             <% java.util.Date date = new java.util.Date(); %>
             The time is now
             <% out.println( String.valueOf( date )); %>
           request is another useful variable
             request.getParameter(“username”): returns value of the
             requested parameter
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke             109
     JavaServer Pages: Example
  <html>
  <head><title>Welcome to B&N</title></head>
  <body>                                       Code embedded
                                                  in HTML
    <h1>Welcome back!</h1>
    <% String name=“NewUser”;
         if (request.getParameter(“username”) != null) {
                 name=request.getParameter(“username”);
         }
    %>                                       • Placing java code
                                                between <% and %>
    You are logged on as user <%=name%>      • The block of code is
    <p>                                         known as scriptlet
  </body>                                    • This makes it possible
                                                to generate dynamic
  </html>                                       HTML pages

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke      110
     Maintaining State
     HTTP is stateless
        Advantages
            Easy to use: don’t need anything
            Great for static-information applications
            Requires no extra memory space
        Disadvantages
            No record of previous requests means
                •   No shopping baskets
                •   No user logins
                •   No custom or dynamic content
                •   Security is more difficult to implement

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   111
     Application State
        Server-side state
            Information is stored in a database, or in the application
             layer’s local memory
        Client-side state
            Information is stored on the client’s computer in the
             form of a cookie
        Hidden state
            Information is hidden within dynamically created web
             pages


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            112
     Server-Side State
     Many types of Server side state:
        1. Store information in a database
            Data will be safe in the database
            BUT: requires a database access to query or update the
             information
        2. Use application layer’s local memory
            Can map the user’s IP address to some state
            BUT: this information is volatile and takes up lots of
             server main memory

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke        113
     Server-Side State
        Should use Server-side state maintenance for
         information that needs to persist
            Old customer orders
            “Click trails” of a user’s movement through a site
            Permanent choices a user makes




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    114
     Client-side State: Cookies
        Storing text on the client which will be passed to
         the application with every HTTP request.
            Can be disabled by the client.
            Are wrongfully perceived as "dangerous," and therefore
             will scare away potential site visitors if asked to enable
             cookies1

        A cookie is a collection of (Name, Value) pairs



Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            115
     Client State: Cookies
       Advantages
          Easy to use in Java Servlets / JSP
          Provide a simple way to persist non-essential data on the
           client (in the browser cache) even after the browser is
           closed
       Disadvantages
          Limit of 4 KB of information (not bad for most applications)
          Users can (and often will) disable them
       Should use cookies to store interactive state
          The current user’s login information
          The current shopping basket
          Any non-permanent choices the user has made
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke            116
     Creating A Cookie
                                                                 Create new
                                                                 cookie object
                                                                 with the specified
     Cookie myCookie =                                           (name, value) pair
      new Cookie(“username", “jeffd");
     response.addCookie(myCookie);
                                                                 Add the cookie to
                                                                 the header of the
                                                                 page


                          You can create a cookie at any time


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                        117
     Attributes of Cookie

     Cookie myCookie = new Cookie(“username”, “jeffd”);
     Cookie.setDomain(www.bookstore.com);
                          // Web site that receives this cookie
     Cookie.setSecure(false):               // no SSL required
     Cookie.setMaxAge(60*60*24*31) // one month lifetime
     response.addCookie(myCookie);

                                   Attributes of the cookie

     SSL - Secured version of HTTP protocol
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke    118
     Cookie - How it works
        We create a cookie through the Java Cookie class in
         the middle tier application code
        The cookie is added to the response object within
         the java servlet to be sent to the client
        Once a cookie is received, the client’s Web browser
         appends it to all HTTP requests it sends to this site,
         until the cookie expires                        Middle
                                                                 tier




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke          119
     Accessing A Cookie
   Look for the cookie with name ‘username’
   Cookie[] cookies = request.getCookies(); // returns an array of cookies
   String theUser;
   for(int i=0; i < cookies.length; i++) {
     Cookie cookie = cookies[i];
     if(cookie.getName().equals(“username”)) theUser = cookie.getValue();
   }
   // at this point theUser == “username”
                                                                 Read the value
                                                                 of the cookie




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                    120
     Hidden State
        Often users will disable cookies
        You can “hide” data in two places:
            Hidden fields within a form
            Using extra path information
        Requires no “storage” of information because the
         state information is passed inside of each web page




Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   121
     Hidden State: Hidden Fields
        Declare hidden fields within a form:
           <input type=‘hidden’ name=‘user’ value=‘username’/>

        Users will not see this information (unless they
         view the HTML source)
        Typically used when we have variables we want to
         pass from one form to another without making the
         user to re-type the information over and over again
           We can use this feature to maintain state, e.g.,
            remember the user in order to update the shopping cart

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke       122
     Hidden State:
     Using Extra Path Information
   The   middle tier can embed an identifier, as
      extra path information, within a document’s
      URL
   As a  user traverse through the site, the
      dynamically generated html pages can pass the
      identifier from document to document
   Thus,   we can track the documents requested by
      the user
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   123
     Hidden State:
     Using Extra Path Information
     Path information                   is stored in the URL request:
              http://server.com/index.htm?user=jeffd
     Can       separate ‘fields’ with an & character:
            index.htm?user=jeffd&preference=pepsi
     There are mechanisms                         to parse this field in
        Java. Check out the
          javax.servlet.http.HttpUtils parserQueryString()
        method.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke              124
     Multiple state methods
   Typically all methods of state maintenance are
   used:
      User logs in and this information is stored in a cookie
      User issues a query which is stored in the path
       information
      User places an item in a shopping cart cookie
      User purchases items and credit-card information is
       stored/retrieved from a database
      User leaves a click-stream which is kept in a log on
       the web server (which can later be analyzed)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke   125
     Summary
     We covered:
        Internet Concepts (URIs, HTTP)
        Web data formats
            HTML, XML, DTDs
        Three-tier architectures
        The presentation layer
            HTML forms, Javascript, Stylesheets.
        The middle tier
            CGI, application servers, Servlets, JavaServer Pages, maintaining
             state (cookies)


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke                   126
     PROJECT: System Requirements
        System requirements:
           Has at least five tables
           Supports a fixed set of at least six query and three
            update operations
           Browser-based user interface
        Software tools
           You may use any DBMS
           MySQL is a popular open source DBMS
               http://dev.mysql.com/downloads/
           If you like C#: Visual Studio
               http://msdn.microsoft.com/en-us/vcsharp/default.aspx


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke        127

								
To top