W1 - Web Applications, Servlets, JDBC Basics
Web Applications Basics
Introduction
1.Web Applications is a software application whose services can be accessed via web.
Eg: While accessing the Amazon over browser, then the Amazon need to be developed and deployed in a server.
2.This communication happened in request and response based using HTTP protocol.
Eg: Web Servers : Apache, IIS Web Clients : Internet Explorer, Chrome, Safari, Firefox
3.Popular Web sites are : Google, Twitter, Facebook.
4. To access a website we use URL(Uniform Resource Locator) which is a compress of protocol,
domain/server name and protocol.
Eg: http://www.google.com:80
http
- Protocolwww.google.com
- Domain/Server Name80
- Port Number
Types of Web Applications
There are two types of web applications.
1. Static
2. Dynamic
Static Web Application:
This is a non interactive web applications, no matters which client needs to access this. There is nothing dynamic or custom happens here for a client.Web Client(Browser) < ————— >Web Server(Image, Multimedia, HTML Page)
Dynamic Web Application:
This is an interactive web application that responds to user inputs and can change its content dynamically. This allows for personalized and real-time updates to the user interface.Web Client(Browser) <—> Web Container(Image, Multimedia, HTMLpage) <—> Database(JDBC)
Server Side Programming
The main role of server-side programming is to handle client requests. The communication between the Web Container and the Database is called Server-Side Programming. This process can be described in four steps.
Step 1 : Receive the request from the client and capture the user's input.
Step 2 : Connect with the database to fetch and retrieve the data requested based on the user's input.
Step 3 : Whatever data comes back from the database, it creates a response page, usually in HTML.
Step 4: Handling the response page in server and sends back the response page to client (Browser)
Web Client(Browser) < —— > Web Container(Image, Multimedia, HTMLpage) < —— > Database(JDBC)
Here web container plays a role of handling the Servlets & JSP’s
Web Application Folder Structure
The first folder is the root folder which contains web application name i.e., WEB APP. This contains home.html, login.jsp, etc
Then the second folder is WEB-INF which have web.xml, classes and lib.
web.xml : The web.xml acts as a interface between developers and web container.
classes : This contains all business logic codes.
lib : All libraries or jar files which will go here. Container search all classes from this jar files from lib.
IDE will not follow the exact folder structure but when we run on server [tomcat] there we can find the below structure
WEB APP : home.html, login jsp, WEB-INF
WEB-INF : web.xml, classes [servlet class file], lib [mysql.jar, spring.jar]
Servlets
Servlet Introduction
Servlets is a technology in Java EE that allows us to develop dynamic web application using Java.
Servlets as API and specification.
API : API stands for Application Program Interface which is used for developer to develop the dynamic web application which is the set of classes and interfaces.
Specification : This is a set of rules which has written how to use the application server or web container for developers. Servers like Apache Tomcat / Web Logic / Web Sphere.
Servlet :
Servlet is a Java program which runs on a web server or web container. It can perform certain operations such as the request comes from the web browser, process it depends on the request data, it can make a call to database perform dynamic operation to fetch and retrieve the data, the data which comes from database converts this into response [creates a HTML page] and sends this response back to the user or developer [Browser].MyServlet.java
Browser
< ——— >Servlet ( Web Container )
< ———— >Database
Servlet Annotations
From servlet 3.0 version, introduces annotations that can be used to configure the various servlet components instead of using web.xml
For example : @webservlet, @webfilter
Life Cycle Methods & Phases
There are 3 main methods for servlets and 4 life cycle phases
Methods: int(), service(), destroy()
Phases : Instantiation, Initialization, Servicing, Destruction
whenever a web application starts, the web container will go through the instantiation phase to create an instance of our servlet class by loading it into memory. this is called the Instantiation phase.
Then here init() method is called after Instantiation phase. This will be call only once by the container. This is the good place to create the database connections [write code here for database connectivity]. This is called as Initialization phase.
Then this service() method will be called after Initialization phase. Here all business and logic codes need to be write so that it will fetch and retrieve data from database. This service() method can be called ‘n’ times . This is called as Servicing phase.
Then this destroy() method will be called. Here this destroy() method kills all the things which are there in init() method. This is also call only once by the service() method. This is called as Destruction phase.
JDBC Basics
JDBC Components
The JDBC architecture consists of four components:
JDBC API
JDBC Drivers
JDBC Client
Driver Manager
JDBC API
Oracle provides two main things in the JDBC API: the API and its specification.
As developers, we learn the JDBC API, which is part of the
java.sql.*
package.This API includes the following classes and interfaces:
Connections
Statements
Result Set
Many more
We use these classes and interfaces in the JDBC Client code to connect with drivers and perform database operations.
The driver connects with databases internally, executes all SQL queries using these interfaces and classes, and processes the results properly.
JDBC Driver
A driver is a software application used between a Java application and a database.
We usually write drivers in Java, and they help implement the JDBC API and JDBC Specification provided by Oracle.
The driver takes instructions from Java and converts them into a format the database understands.
After executing SQL statements, it takes the response from the database, converts it, and sends it back to the Java application.
Java Client Application
< ——— > Driver
< ———— > Database
JDBC Client
This is a program we are developing to connect with a database for standalone Java or Java EE applications. It performs the following tasks:
Connects to the database
Performs CRUD operations using SQL statements
Processes the response
Handles exceptions in the JDBC Client code (including transaction management)
Closes the database connection.
JDBC Client
Driver manager
A Driver Manager is one of the key classes in the JDBC API. It acts as a helper between the Driver and the Java application. The Java application uses the Driver Manager once to find the right driver to connect to the database. It establishes the connection through the driver and returns the connection to our Java client.
Our Java client directly communicates with the driver to execute all SQL operations.
Java Client
< — > Driver Manager
< ———— > Driver
< ———— > Database