Beruflich Dokumente
Kultur Dokumente
Chapter 2: Java EE Related Technologies Syllabus: 1. Java Database Connectivity 2. Servlets 3. JavaServer Pages 4. Java Server Faces 5. JavaMail 6. Enterprise JavaBeans 7. Hibernate 8. Seam 9. Java EE Connector Architecture 10. Web Services 11. Strut 12. Spring 13. JAAS 14. AJAX
Figure 2-1: Architecture of JDBC As shown in Figure 2-1, the Java application that wants to communicate with a database has to be programmed using JDBC API. The JDBC driver (third party vendor implementation) supporting datasource such as Oracle, and SQL, has to be plugged in to the drive, which can be done dynamically at runtime. This ensures that the Java application is vendor independent. Therefore, if we want to communicate with any data source through JDBC, we need a JDBC driver, which intelligently communicates with the respective data source. Currently, we have 720 plus JDBC drivers available in the market, which are designed to communicate with different data source. Some of these drivers are Pure Java drivers and are portable for all the environments whereas others are Partly Java drivers and require some libraries to communicate with the database. We need to understand the architectures of all the four types of drivers to decide which driver to use to communicate with our required data store. Let's now learn about each of these drivers in detail. JDBC Drivers The different types of drivers available in JDBC are listed in below: JDBC Drive Types Description Type-1 Driver This type of driver is Bridge Driver (JDBC-ODBC bridge) Type-2 Driver This driver is a Partly Java and Partly Native code driver Type-3 Driver This is a Pure Java driver that uses a middleware driver to connect to a database Type-4 Driver This is a Pure Java driver and is directly connected to a database
Figure 2-2: The architecture of the Type-1 driver Figure 2-2 shows the architecture of the system that uses JDBC-ODBC bridge driver to communicate with the respective database. As shown in the figure Java Application is programmed to JDBC API and will make a request to JDBC-ODBC bridge driver i.e. JDBC call, where JDBC-ODBC bridge driver is responsible to resolve the JDBC call and make an equivalent ODBC calls to the ODBC driver. To do this JDBC-ODBC bridge driver uses ODBC API. Later ODBC driver comes into action and completes the request, and sends response to the JDBC-ODBC bridge driver. Now, JDBC-ODBC bridge driver converts the response and presents it to the requesting Java Application in JDBC standards. This type of driver is generally used in the development and testing phases of Java applications. Advantages of Type-1 Driver Single Driver implementation can be used to interact with different data stores. This driver allows us to communicate with all the databases supported by the ODBC driver. This is a vendor independent driver. Disadvantages of Type-1 Driver Due to a large number of translations, the execution speed is decreased. This driver depends on the ODBC driver, and therefore, Java applications also become indirectly dependent on ODBC drivers. ODBC binary code (or ODBC client library) must be installed on every client. This driver uses JNI to make ODBC calls. Because of the preceding listed disadvantages, this driver is not recommended to be used in production environment. In case a database does not have any other JDBC driver implementations, then only we should use this driver in the production environment. Describing Type-2 Driver (Java to Native API)
Figure 2-3: The architecture of the Type-2 driver As shown in Figure 2-3, the Java application that wants to communicate with the database is programmed using JDBC API. These JDBC calls (programs written by using JDBC API) are converted into database specific native calls in the client machine and the request is then dispatched to the database specific native libraries. These native libraries present in the client are intelligent to send the request to the database server by using native protocol. This type of driver is implemented specific to a database and is usually delivered by a DBMS vendor. However, it is not mandatory that Type-2 drivers have to be implemented by DBMS vendors only. An example of Type-2 driver is the Weblogic driver implemented by BEA Weblogic. Type-2 drivers are suitable to use with server-side applications. It is not recommended to use Type-2 drivers with client-side applications, since the database specific native libraries for the client platform should be installed on the client machines. Advantages of Type-2 Driver This driver helps in accessing the data faster as compared to other type of drivers We can use the additional features provided by the specific database vendor, which are even supported by the JDBC specification. Disadvantages of Type-2 Driver Native libraries must be installed on client machines since the conversion from JDBC calls to database specific native calls is done on the client machine. Database specific native functions are executed on the client JMV process and any bug in this driver can crash the JVM. It may increase the cost of the application if the application needs to run on different platforms (since we may require buying the native libraries for all of the platforms). Examples of Type-2 Driver OCI (Oracle Call Interface) DriverOracle Corporation has implemented this driver to communicate with the Oracle database server. This driver converts JDBC calls into OCI (Oracle native libraries) calls. Weblogic OCI Driver for Oracle This driver is also similar to the oracle OCI driver, but the Weblogic OCI JDBC driver makes JNI calls to weblogic library functions. Type-2 Driver for Sybase This driver converts JDBC calls into Sybase dblib or ctlib calls, which are native libraries to connect to Sybase.
Figure 2-4: The architecture of the Type-3 driver As shown in Figure 2-4, JDBC Type-3 driver listens for JDBC calls from the Java application and translates them into middleware server specific calls. Then, the driver communicates with the middleware server over a socket. The middleware server converts these calls into database specific calls. This type of drivers are named as net-protocol fully Java technology-enabled driver, or simply net-protocol driver. The middleware server can be added with some additional functionalities, such as pool management, to improve the performance and availability of connection, which can make this architecture more useful in enterprise applications. Type-3 driver is recommended to be used with applets since this type of driver is auto downloadable. Advantages of Type-3 Drivers Type-3 drivers are pure Java drivers and are auto downloadable. No native libraries are required on the client machine. A single driver provides accessibility to multiple databases, implying that this driver is database independent. Database details, such as username, password, and database server location, are not required to be given to the client; instead, they are configured in the middleware server. We can switch over from one database to other without changing the client-side driver classes, by just changing the configurations of the middleware server. Disadvantages of Type-3 Driver Compared to Type-2 drivers, Type-3 drivers are slow due to the increased number of network calls. Type-3 drivers are costlier compared to the other drivers. Examples of Type-3 Drivers IDS Driver This driver listens for JDBC calls and converts them into IDS Server specific understandable network calls. A Type-3 driver communicates over a socket to IDS Server, which acts as a middleware server (as described in the architecture). Weblogic EMI Driver This driver listens for JDBC calls and sends the request from the client to the middleware server by using the RMI protocol. The middleware server uses a suitable JDBC driver to communicate with a database.
Figure 2-5: The architecture of the Type-4 driver As shown in Figure 2-5, the Type-4 driver prepares a DBMS specific network message and then communicates with database server over a socket. This type of driver is lightweight and is generally known as thin driver. The Type-4 driver uses data base specific proprietary protocols for communication. Generally, this type of drivers are implemented by DBMS vendors, since the protocol used here are proprietary. You can use Type-4 drivers when you want an auto downloadable option for the client-side applications. It is also recommended to be used with server-side applications. Advantages of Type-4 Driver This type of drivers are pure Java drivers and hence auto downloadable. No native libraries are required to be installed in the client machine. Secure to use since it uses database server specific protocol. This type of driver does not require a middleware server. Disadvantage of Type-4 Driver The main disadvantage of Type-4 Driver is that it uses database specific proprietary protocol and is DBMS vendor dependent. Examples of Type-4 Driver Thin Driver for Oracle from Oracle Corporation Weblogic, Msssqlserver4 for MS SQL Server from BEA Systems
Servlets Java Servlets technology provides a simple, vendor independent mechanism for extending the functionality of a Web server. This technology provides high level, component-based, platformindependent, and senwr-independent standards to develop Web applications in Java. The Servlet technology is similar to other scripting languages, such as Common Gateway Interface (CGI) scripts, JavaScript (on the client side), and PHP. However, Servlets are more acceptable since they overcome the limitations of CGI, such as low performance and scalability. What is a Servlet? A Servlet is a simple Java class, which is dynamically loaded on a Web server and thus enhances the functionality of the Web server. A Servlet is secure and portable as it runs on Java Virtual
Hibernate Hibernate provides a framework for mapping an object-oriented domain model to a relational database. The primary feature of Hibernate is to map JavaBean classes to database tables. Hibernate helps in querying and retrieving data from database tables. In addition, Hibernate generates the SQL calls and so the developers do not need to provide Java code for handling resultsets. Hibernate ensures that a Web application is portable with all SQL supported databases. Hibernate also provides persistence feature for Plain Old Java Objects (POJOs). Hibernate can be used both standalone Java applications and in Java EE applications using Servlets or EJB session beans. Introduction to Hibernate Hibernate is a lightweight Object/Relational mapping (ORM), which is a technique for mapping an object model to a relational model. Hibernate handles mapping from Java classes to database tables, and provides API for querying and retrieving data from database. This helps to write sophisticated query by using Hibernate Query Language (HQL) that reduces the time spent in handling data by using SQL and JDBC. It relieves the developer from 95 percent of a common data persistence related programming tasks, compared to manual coding with SQL and the JDBC API. Hibernate uses a transparent programming model for mapping Java objects to the relational databases. You only need to write a simple POJO (Plain Old Java Object) and create an XML mapping file that describes relationship between the database and the class attributes by using
10
11
Figure: The Hibernate architecture Hibernate Query Language Hibernate Query Language (HQL) is an easy-to-learn and powerful query language designed as an Object-oriented extension to SQL that bridges the gap between the Object-oriented systems and relational database. The syntax of HQL is very similar to SQL. It has a rich and powerful Objectoriented query language available with the Hibernate object/relational mapping (ORM). The term ORM refers to the technique of mapping objects and databases. The data from Object-oriented systems are mapped to relational databases with a SQL-based Schema HQL queries are case-insensitive, but the names of Java classes and properties are case-sensitive. It is used to execute queries against database. Hibernate automatically generates the SQL query and executes it, if HQL queries are used in the application. HQL uses classes and properties instead of tables and columns. HQL supports polymorphism and associations and requires less coding than SQL. It also supports many other SQL statements and aggregate functions, such as sum(), max() and clauses, such as group by and order by. Learning HQL is much easier as compared to SQL. The other options that can be used while using Hibernate are Query By Criteria (QBC) and Query By Example (QBE) using Criteria API and the Native SQL queries. It even supports projection/aggregation and subselects. HQL is used for retrieving objects from database through O/R mapping. Thus, HQL provides the following: Applying restrictions to properties of objects Ordering the results returned by a query Pagination of results Aggregation using group by, having clauses Joins User-defined functions Subqueries
12
13
14
15