Servlets: Tiers, Database Connectivity and Beans
Notes available courtesy of Joel Budgor

student email of interest

Objectives

    Describe the strengths / weakness of different distributed computing
    models.

    List different distributed technologies supported by Java.

Distributed computing models.

    To what does the term distributed computing refer?

        Computer systems whose components are distributed across several hosts.

    What are the class distributed computing models?

        2 Tiered architecture.

        N Tiered architecture.

    What is a 2 tiered architecture.

        A system combining 2 software components.

            Client

                Makes requests of server.

                Presents server response back to end user.

            Server

                Receives client requests.

                Process data from client requests.

                Returns response to client.

        Historically, what are some of the benefits associated with
        2 tiered architectures?

            ???

            Simplicity.

            Fewer points of failure.

        Historically, what are some of the problems associated with
        2 Tiered architectures?

            ???

            For applications requiring drivers (ie. database) drivers
            must be distributed / maintained on clients.

                This makes it harder to maintain or change the
                configuration of the system.

            In order to scale the application, some of the processing
            power may have to be placed on the client.

                This makes it harder to maintain or change the
                configuration in large organizations.

            Less fault tolerant.

            Lack of portability.

                Must use cut / paste techniques to separate business
                logic from user interface.

        Review graphic

    What is an N tiered architecture.

        A system combining N software components.

            Client

                Makes requests of server.

                Presents server response back to end user.

            Middleware server.

                Receives client requests.

                Processes data before forwarding to a back end
                server.

                Receives server responses before forwarding them
                back to the client.

            Back End Server

                Receives requests from middleware server.

                Process / store data from client requests before
                forwarding them back to middleware server.

            This is a 3 tiered architecture.

        Historically, what are some of the benefits associated with
        N tiered architectures?

            For applications requiring drivers (ie. database) drivers
            can be distributed / maintained on middleware.

                This makes it easier to maintain or change the
                configuration of the system.

            In order to scale the application, some of the processing
            power can have to be placed on the client.

                This makes it easier to maintain or change the
                configuration in large organizations.

            Greater fault tolerance.

                If a middleware server goes down, other middleware
                servers can pick up the load transparently.

            Modularity / Recyclability.

                Middleware may port to different clients.

        Historically, what are some of the problems associated with
        N Tiered architectures?

            Complexity

            More points of failure.

        Review graphic

What distributed computing technologies does Java support?

    Client side.

        Applet

            Client side programs that enhance what a browser can do.

        HTTP

            HyperText Telecommunications Protocol.

            Web protocol.

        Javascript

            Client side scripting language that works with HTTP.

    Server side.

        Servlet

            Server side programs that enhance what a server can do.

        JSP

            Java Server Pages.

            Server side web pages that include programming elements.

        JDBC

            Java Database Connectivity.

            API that provides access to SQL databases.

        RMI

            Remote Method Invocation.

            Native Java distributed object technology.

                Objects live on one host, but pretend to live on others.

        Corba

            Common Object Request Broker Architecture.

            Non-Java distributed object technology.

        EJB

            Enterprise JavaBeans

            Server side technology that provides transparent access to
            underlying services.

What is JDBC?

    API. java.sql

    Provides a standard interface to most SQL database servers.

        Standard methods supported by most SQL servers.

        Low level interface.

            Applications programmer :

                Builds standard SQL queries manually as strings.

                Submits the SQL strings to appropriate API methods.

                API methods deliver the information to drivers provided
                by the database vendor.

                Drivers process and forward the information to the
                database server for native processing.

An SQL Primer see notes

    Below are examples of some common SQL expressions.

        INSERT

insert into table_name
 (
     field_1
     [, field_2, ..., field_n ]
 )
 values
 (
     value_1 [, ..., value_n ]
 )

insert into students
(
    stu_id, stu_first_name
)
values
(
    1, 'Joel'
)

        SELECT

select
    [ table_name.field_name [ , ... ] ]
    from table_name [ , ... ]
    [ where table_name.field_name operator value
        [ and | or table_name.field_name operator value ... ] ]
    [ order by table_name.field_name [ desc ], ... ]

select cou_id, cou_name
     from courses
     where cou_name LIKE '%Java%'
     order by cou_name

        UPDATE

update table_name
    set field_name = value [ , ... ]
    [ where field_name operator value
        [ and | or field_name operator value ... ] ]

update classes
     set grade = 99
     where cou_name = 'Client / Server Programming'
         and cou_sem = 'Fal'
         and cou_yr = 1997
 

        DELETE

delete from table_name
    [ where field_name operator value
        [ and | or field_name operator value ... ] ]

delete from courses
     where cou_name LIKE '%Java%'

An example of a database accessing servlet DBServlet.java and another variation here (HttpServlet and service method )

While DBServlet demonstrates how to create a basic database access servlet, it functions as if performance is unimportant by creating a new connection for every request. It generally takes one to three seconds to establish a database connection. Also, small, frequent queries only require short connection times to be efficient.

Concurrency is also a common problem when handling multiple users accessing the same data. When accessing a database, all connections must be thread safe to prevent data corruption or lock-ups.
DBServlet's connections are thread safe because they are created as local variables within the service routine. However, creating a new connection for each request is inefficient.

The best solution to the timing and connection problem is to create a pool of persistent (reusable) connections that will be used by a servlet as needed. This pool of connections should be managed by a separate object called a connection pool.

A connection pool should manage a set of connections, watch for locked or corrupted connections, perform housekeeping tasks (such as maintaining arrays of connections available or in use), and possibly log events.

Items to consider:

Thus, two components of a basic connection servlet solution should be

  1. the Servlet (connection pool client)
  2. a Connection Pool

The connection pool and connection pool servlet are middle tiers.

All in all, the servlet would:

Where should each of these be ... in the init, service, or destroy?

A DBServlet using a ConnectionPool here

Current Application servers may already provide connection pools.
In Java SE 5.0 javax.sql has a PooledConnection Interface and ConnectionPoolDataSource Interface.

Hall's pdf JDBC and Database Connection Pooling link changed - was to 1st edition of book - so actually this link is now here
and Sun Developer Network (SDN) chapter for Connection Pooling (from book)

Servlet Beans

Actually this is not big deal ... it is simply a servlet that is a bean.

What does this imply?

Some considerations (dependant on web server)

An Example Writing Servlets as Beans and the J2EE tutorial notes with Beans (from the source)
also pdf from Hall book

FYI: BeanContext part of the JavaBeans tutorial