Explain in detail about J2EE multi tier architecture
J2EE is four-tier architecture. These consist of Client Tier (Presentation tier or Application tier), Web tier, Enterprise JavaBeans Tier(or Business tier), and the Enterprise Information Systems Tier. Two or more tiers can physically reside on the same Java Virtual Machine although each tier provides a specific type of functionality to an application. Some of the APIs of J2EE components can be used on more than one tier (i.e. XML API), while other APIs (i.e., EJB API) or associated with a particular tier.
CLIENT TIER Client tier consists of programs that interact with the user. It prompts the user for input and then convert the users response into requests that are forwarded to software on a component that processes the request and returns results to the client program. J2EE clients can be classified as follows Web client is a A software(usually browser) that accesses resources located on the web tier. Ejb client can access one or more enterprise javabeans that are located on the EJB tier rather than resources on the web tier. EIS clients are the interface between users and resources located on the EIS tier. Multi-tier clients can access components located on tiers other than the tier where the multi-tier client resides.
WEB TIER Web tier accepts requests from other software that was sent using POST, GET, and PUT operations, which are part of HTTP transmissions. The two major components of web tier are Servlets and Java Server Pages. A servlet is a java class that resides on the web tier and is called by a request from a browser client that operates on the client tier. A servlet is associated with a URL that is mapped by the servlet container. It typically generates an HTML output stream that is returned to the web server. The web server in turn transmits the data to the client. JSP is different than a servlet depending on the container that is used. JSP uses custom tags to access the bean. ENTERPRISE JAVA BEANS TIER Enterprise java bean is a class that contains business logic and callable from a servlet or Jsp. EJB tier contains the enterprise java beans server that stores and manages enterprise java beans. This tier automatically handles concurrency issues that assure multiple clients have simultaneous access to the same object and also manages instances of components. EJB server and EJB container is responsible for low level system services that are essential for implementing business logic. ENTERPRISE INFORMATION SYSTEMS TIER This tier provides flexibility to developers of J2EE applications since it include variety of resources and support connectivity to resources. It defines all the elements that are needed to communicate between J2EE application and non-J2EE software. The following figure shows the tiers of J2EE that provides a specific function to an application.
JDBC Process:
There are seven steps to use JDBC:-
1. Import package:- java.sql package must be imported into the program.
2. Load the JDBC driver:- Next, we must either register or load the appropriate driver, for loading, we use Class.forName() method to which the name of the driver to be loaded is passed as argument.
Eg: Class.forName (sun.jdbc.odbc.JdbcOdbcDriver); for registering the driver, we use the static method of DriverManager class ] i.e.registerDriver. Eg:DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
3. Connect to the database:- In order to connect to the database, we use getConnection() method which is another static method of DriverManager class. Syntax:- DriverManager.getConnection(String URL, String user, String password); BROWSER Component Component Client tier Component Component Component Component EJB Component Component Component DBMS Web tier Ejb tier Eis tier where URL contains driver name and name of database, URL is usually of the form: protocol:subprotocol:subname where protocol is always jdbc, subprotocol is the name of database connectivity mechanism. Eg: odbc, and subname is DSN.
User and password are the username and password of the database user respectively.
NOTE: Not all databases require a username and password for each user ( so that they can access the database), some grant accesses to any user Eg: MS Access.
The getConnection() returns a Connection object which represents a connection to database.
4. Create Statement object:-
Depending on the type of statement object to be created, the following methods are used:-
i. To createStatement object:- con.createStatement(); where con is a connection object. This method returns a Statement object for sending SQL statement to database. con.createStatement(int rs,int rsc); where rs and rsc represents the type and concurrency type of the ResultSet object to be returned when the statement object is executed. ii. To create PreparedStatement object:- con.prepareStatement(String s); con.prepareStatement(String s,int rs,int concur);
the first form receives the SQL statement as an argument and returns a PreparedStatement object that will generate ResultSet statement object with general type (rs) and concurrency (concurr). iii. To create CallableStatement object:- con.prepareCall(String s); con.prepareCall(String s,int r, int concur) ;
the first form receives the SQL statement as an argument and returns a CallableStatement object that will generate ResultSet statement object with general type (rs) and concurrency (concurr).
5. Execute the statement object:-
There are various methods of Statement interface:- . s.execute(); where s is a Statement object. This method is used to execute SQL statements when there are multiple results returned .It returns true if the first object is ResultSet object and the ResultSet can be retrieved using getResultSet() of Statement interface. i. s.executeQuery(String s); This method is used to execute an SQL statement (passed as an argument), which will return in one ResultSet object. ii. s.executeUpdate(String St); This is used to execute the INSERT, UPATE and DELETE queries. This returns an integer, which represent the number (rows) which are affected. This method is also used for DDL statements 6. Retrieve Results:- The results returned after executing the statement object are ResultSet. The ResultSet interface provides various methods to manipulate the result. 7. Closing the statement and connection:- The statement object can be closed by:- s.close(); s is a statement object to be closed. The connection can be closed by:- con.close(); con is a connection object to be closed.