You are on page 1of 41

Enterprise

Java
Unit 1
Objectives

Understanding Java EE
Java EE Architecture, Server and
Containers
Introduction to Java Servlets
Servlet API and Lifecycle
Working With Servlets
Working with Databases
Understanding Java EE

What is an Enterprise Application?


Enterprise Applications (EA) are a software solution that provide
business logic and tools to model entire business processes for
organisations to improve productivity and efficiency.
What is ?

The Java EE platform is built on top of the Java SE platform.


The Java EE platform provides an API and runtime environment
for developing and running large-scale, multi-tiered, scalable,
reliable, and secure network applications.
Java EE Technologies

Web Applications Technologies

Enterprise Application Technologies

Web Services Technologies

Security Technologies
Java EE Evolution

Java Platform, Enterprise Edition (Java EE), formerly Java 2 Platforms, Enterprise
Edition (J2EE), currently Jakarta EE, is a set of specifications, extending Java SE
with specifications for enterprise features such as distributed computing and
web services.
GlassFish Server

GlassFish is an open source community comprising of users, developers,


parteners that are developing an open source application server that
implements Java EE.
It's a Web server & servlet container
Types of Architecture:

One Tier Architecture:

One-tier architecture has all the layers such


as Presentation, Business, Data Access layers
in a single software package.
Simplest and most direct.
Types of Architecture:

Two-Tier Architecture:

The Two-tier architecture is divided into two


parts:
1. Client Application (Client Tier)
2. Database (Data Tier)
The client system handles both Presentation
and Application layers and the Server system
handles the Database layer.
Types of Architecture:

Three-Tier Architecture:

The three-tier architecture allows any one of


the three tiers to be upgraded or replaced
independently.

The Three Tiers in a Three-Tier Architecture

Presentation layer (Client Tier)


Application layer (Business Tier)
Database layer (Data Tier)
Types of Architecture:

Multi-Tier Architecture:

N-Tier application AKA Distributed


application
It is similar to the three-tier architecture but
the number of application servers is
increased and represented in individual tiers
in order to distribute the business logic so
that the logic will be distributed.
JAVA EE SERVERS

A Java EE server is a server application that the implements the Java EE


platform APIs and provides the standard Java EE services.Java EE servers
are sometimes called application servers, because they allow you to serve
application data to clients, much like web servers serve web pages to web
browsers.
JAVA EE CONTAINERS

Containers are the interface between a component and the low-level,


platform-specific functionality that supports the component.

EJB container
Web container
Application client container
Applet container
The Need for
DYNAMIC CONTENT

Better Performance Portability Robust


Servlet can be described in many ways,

Servlet depending on the context.

Technology Servlet is a technology which is used


to create a web application

Servlet is an API that provides many


interfaces and classes.

Servlet is an interface that must be


implemented for creating any Servlet.
Why Servlets

Before Servlet, CGI


Java Servlets are an (Common Gateway
Servlets are always
efficient and powerful Interface) scripting
part of a larger project
solution for creating language was
called a Web
dynamic content for common as a server
Application.
the Web. -side programming
language. .
What can Servlets do?

A servlet is a Java programming language class that is used to extend the


capabilities of servers that host applications accessed by means of a request-
response programming model.

Although servlets can respond to any type of request, they


are commonly used to extend the applications hosted by web
servers. For such applications, Java Servlet technology defines
HTTP-specific servlet classes.
Servlet
Lifecycle
Diagram
Servlet Lifecycle
•The init() method: Initializes the servlet invoked by the web
container only once.
•The service() method: Provides response for incoming requests.
Invoked at each request by the web container.
1. doGet()
2. doPost()
•The destroy() method: Invoked only once and indicates that the
servlet is being destroyed. The object of the servlet is then marked
for garbage collection.
Method Definition
•Init () method : public void init() throws ServletException {
// Initialization code... }
•Service() method: doGet() method:-
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code }
•Service() method: doPost() method:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code }
•Destroy() Method:
public void destroy() {
// Finalization code... }
Simple Welcome Servlet
WelcomeServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");//setting the content type
PrintWriter pw=res.getWriter();//get the stream to write the data

//writing html in the stream


pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");

pw.close();//closing the stream


}}
Working with Servlets
Deployment
Descriptor
The deployment descriptor is
an XML file

This is how the Web Container


gets the information about the
servlet to be invoked.
Why use They reduce the need to deal
with deployment descriptors.
Annotations
Annotations start with '@' sign .

Help to associate metadata


(information) to the program
elements.

They can change the way a


program is treated by compiler.
Java Servlet API
There are two packages

javax.servlet :- package that contains the classes to support


generic servlet

javax.servlet.http:- package that contains classes to support


http servlet

java.lang.Object
|_extended byjavax.servlet.GenericServlet
|_extended byjavax.servlet.http.HttpServlet
HTTP Servlet
Http Servlet must extend javax.servlet.http.HttpServlet class, which is an
abstract class
doGet() – This method is called by servlet service method to handle the HTTP
GET request from client. The Get method is used for getting information from
the server
doPost() – Used for posting information to the Server
doPut() – This method is similar to doPost method but unlike doPost method
where we send information to the server, this method sends file to the server,
this is similar to the FTP operation from client to server
doDelete() – allows a client to delete a document, webpage or information
from the server
init() and destroy() – Used for managing resources that are held for the life of
the servlet
getServletInfo() – Returns information about the servlet, such as author,
version, and copyright.
What is JDBC?
•JDBC stands for Java Database Connectivity, which is a standard Java API
for database-independent connectivity between the Java programming
language and a wide range of databases.

•JDBC is a tool or an application programming interface that establishes


a connection between a standard Database and Java application that
intends to use that database
•The JDBC library includes APIs for each of the tasks mentioned
below that are commonly associated with database usage.
•Making a connection to a database.
•Creating SQL or MySQL statements.
•Executing SQL or MySQL queries in the database.
•Viewing & Modifying the resulting records.
The JDBC Driver types
A JDBC driver is a set of Java classes that implement
the JDBC interfaces, targeting a specific Database

The JDBC interfaces comes with standard Java

Implementation of these interfaces is specific to the


Database
Types of JDBC Drivers
type 1 JDBC-ODBC Bridge

type 2 Native-API Driver

type 3 Network-Protocol driver (Middleware driver)

type 4 Database-protocol driver (Pure Java driver)


Type 1: JDBC-ODBC Bridge Driver

JDBC API

Vendor
Java JDBC-ODBC ODBC Data
database
application bridge driver Driver base
library
Type 1: JDBC-ODBC Bridge Driver
Advantages
It is very easy to use
Almost any database is supported
can be easily connected to any database

limitations
Performance will not be efficient
ODBC Driver needs to be installed
Type 1 drivers are not portable
Not suitable for Applets
Type 2 Native-API Driver

JDBC API

Vendor
Java Native API database
Data
application Driver
library base
Type 2 Native-API Driver
Advantages
Faster than type 1 Driver

limitations
Clients Side Library is not available for all databases
Vendor Client Library needs to be installed
It is a Platform Dependent
Not Thread Safe
Type 3: Network Protocol Driver
ADVANTAGES LIMITATIONS

No additional library Performance will be slow


installation is required on Requires Database-
client side specific coding
No changes are required at Maintenance of network
client for any Database protocol driver becomes
A single driver can handle costly
any data provided the
middleware supports it
Type 4: Database Protocol
Driver
ADVANTAGES LIMITATIONS

Platform Independent
No Intermediate format is
required Drivers are database
Application connects dependent.
directly to the database
server

You might also like