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
- Protocol
www.google.com - Domain/Server Name
80 - Port Number

Types of Web Applications

There are two types of web applications.
1. Static
2. Dynamic

  1. 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)

  2. 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

  1. The first folder is the root folder which contains web application name i.e., WEB APP. This contains home.html, login.jsp, etc

  2. Then the second folder is WEB-INF which have web.xml, classes and lib.

    1. web.xml : The web.xml acts as a interface between developers and web container.

    2. classes : This contains all business logic codes.

    3. lib : All libraries or jar files which will go here. Container search all classes from this jar files from lib.

  3. IDE will not follow the exact folder structure but when we run on server [tomcat] there we can find the below structure

    1. WEB APP : home.html, login jsp, WEB-INF

    2. WEB-INF : web.xml, classes [servlet class file], lib [mysql.jar, spring.jar]

Servlets

Servlet Introduction

  1. Servlets is a technology in Java EE that allows us to develop dynamic web application using Java.

  2. Servlets as API and specification.

    1. 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.

    2. 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.

  3. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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:

  1. JDBC API

  2. JDBC Drivers

  3. JDBC Client

  4. Driver Manager

JDBC API

  1. Oracle provides two main things in the JDBC API: the API and its specification.

  2. As developers, we learn the JDBC API, which is part of the java.sql.* package.

  3. This API includes the following classes and interfaces:

    1. Connections

    2. Statements

    3. Result Set

    4. Many more

  4. We use these classes and interfaces in the JDBC Client code to connect with drivers and perform database operations.

  5. The driver connects with databases internally, executes all SQL queries using these interfaces and classes, and processes the results properly.

JDBC Driver

  1. A driver is a software application used between a Java application and a database.

  2. We usually write drivers in Java, and they help implement the JDBC API and JDBC Specification provided by Oracle.

  3. The driver takes instructions from Java and converts them into a format the database understands.

  4. 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:

  1. Connects to the database

  2. Performs CRUD operations using SQL statements

  3. Processes the response

  4. Handles exceptions in the JDBC Client code (including transaction management)

  5. 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