Subject Title: Advance Java Programming,
Code: CSF206, Credit: [Link]
Class: [Link]. 2st Year, 4th Sem
UNIT IV: Enterprise programming
Introduction to J2EE and J2ME, Overview of MVC, Servlets and JSP
Demonstration of short Projects
J2EE stands for Java 2 Platform, Enterprise Edition. J2EE is the
standard platform for developing applications in the
enterprise and is designed for enterprise applications that run
on servers.
J2EE provides APIs that let developers create workflows and
make use of resources such as databases or web services.
J2EE consists of a set of APIs. Developers can use these APIs
to build applications for business computing.
J2EE can be expanded as Java 2 Enterprise Edition that offers a
development environment for enterprise application creation
and implementation.
J2EE Architecture is made up of three tier, such as the client
tier that is used as an interactive medium for the end user or
the client & consists of web clients and application clients, the
middle tier that is used for defining logical functioning units &
consist of web components and EJB components, and the
enterprise data tier that is used for storage purposes in the
form of relational database & consists of containers,
components and services.
The client tier consists of programs or applications interact
with the user. Usually, they are located in a different machine
from the server. Client tier prompts the user inputs into user
requests then forwarded to the J2EE server then processed
result returned back to the client. A client can be a web
browser, standalone application or server that runs on a
different machine.
Clients can be classified as a Web Client and Application
Client.
Acts as front end for an application.
Acts as a user interface for application.
Get the input from user and then convert into
requests that are forwarded to middle
tier(server).
It translates the server’s response into text
and presented to the user.
A web browser, a stand-alone application, or
other servers.
Firewalls are implemented on the client tier to
secure the applications.
Consists of one or more sub-tiers
It process the request, generates the reponse and gives it to
the client tier.
Application server or web server
Various containers for processing the request
Web container(Web tier)
EJB container(EJB tier)
Web container(Web tier)
Used for deploying web applications
Provides internet functionality for J2EE application.
Components-Servlets, JSP, HTML or XML
EJB container
Used to deploy business applications
Contains the collection of Enterprise Java Beans(EJB)
Enterprise Java Bean contains business logic of applications
Responsible for low-level system services required to
implement business logic of an Enterprise java bean.
System services
Resource pooling
Distributed Object Protocols.
Thread Management
State Management
Process Management
Object Persistence
Security
Deploy –time configuration
Enterprise data is stored in a relational database. This tier
contains containers, components and services.
Enterprise components handle usually business code that is logic
to solve particular business domains such as banking or finance
are handled by enterprise bean running in the business tier.
Technologies used in EIS Tier:
Java Database Connectivity API (JDBC).
Java Persistence API.
Java Connector Architecture.
Java Transaction API.
Provides the connectivity between a java EE
application and non-java EE application.
Include a variety of resources
Database Servers
Enterprice Resource Planning(ERP) systems,
Legacy Systems
It should be the back end for an
application
Provides flexibility to java EE developers.
CORBA, Java Connectors or through
proprietary protocols.
Portability: If you build a J2EE application on a specific
platform, it runs the same way on any other J2EE-compliant
platform. This makes it easy to move applications from one
environment to another.
Reusability: The components in J2EE are reused, so the
average size of an application is much smaller than it would
be if you had to write equivalent functionality from scratch for
each program.
Security: Java technology lets programmers handle sensitive
data far more securely than they can in C/C++ programs.
Security: Java technology lets programmers handle sensitive
data far more securely than they can in C/C++ programs.
Scalability: J2EE lets developers build applications that run
well on both small, single-processor computers and large,
multi-processor systems.
Reliability: Many of the services (such as transaction
management and monitoring) that applications need to be
reliable are built into J2EE.
When a client computer sends a request to a server, in simple
terms, when you type in a URL on your browser, the request
ultimately reaches another computer, the 'server', which
serves the particular web page you requested.
Servlet is a program that runs on that web server that can
process your request and send the right content back. These
programs have a life cycle from creation to destruction.
When we search on google for [Link], we make a
request to a server to render the web page of dituniversity.
Handling such requests and rendering web pages dynamically
can be done using Java Servlets.
So what exactly are servlets? Java Servlets (also known
as Jakarta Servlets) are server-side programs that run on a
web server, acting as a middle layer between client requests
and database/applications on the web server. They are used
to host web applications on servers and mostly support
all client-server protocols.
Client/Browser: The client through the browser sends HTTP
requests to the webserver.
Web Server: Includes several components to control access to
files hosted on the server. A basic HTTP server will
comprehend URLs and follow HTTP protocol to deliver the
contents of hosted websites.
Web Container: This is a web server component that interacts
with Java servlets. It manages the servlets' life cycle, including
loading, unloading, managing request and response objects,
and URL mapping on the server-side. Ex- Tomcat.
The web container maintains the life cycle of a servlet
instance.
Stages of the Servlet Life Cycle: The Servlet life cycle mainly
goes through four stages,
1. Loading a Servlet.
2. Initializing the Servlet. [init()]
3. Request handling. [service()]
4. Destroying the Servlet. [destroy()]
The Life Cycle of Servlet is as follows:
Servlet class is loaded first when the Web container receives a new
request.
Then the web container creates an instance of the servlet. This
instance is created only once in the whole life cycle of a servlet.
The servlet is initialized by the calling init() method.
service() method is called by the servlet to process the client's
request.
Servlet is destroyed by calling the destroy() method.
Java Virtual Machine's(JVM) garbage collector clears the
destroyed servlet's memory.
The first stage of the Servlet lifecycle involves loading and
initializing the Servlet by the Servlet container.
The Web container or Servlet Container can load the Servlet at
either of the following two stages :
Loading : Loads the Servlet class.
Instantiation : Creates an instance of the Servlet. To create a
new instance of the Servlet, the container uses the no-
argument constructor.
After the Servlet is instantiated successfully, the Servlet
container initializes the instantiated Servlet object.
The container initializes the Servlet object by invoking
the [Link](ServletConfig) method which accepts
ServletConfig object reference as parameter.
The Servlet container invokes
the [Link](ServletConfig) method only once, immediately
after the [Link](ServletConfig) object is instantiated
successfully. This method is used to initialize the resources,
such as JDBC datasource.
After initialization, the Servlet instance is ready to serve the client
requests. The Servlet container performs the following operations
when the Servlet instance is located to service a request :It creates
the ServletRequest and ServletResponse objects. In this case, if this
is a HTTP request, then the Web container
creates HttpServletRequest and HttpServletResponse objects which
are subtypes of the ServletRequest and ServletResponse objects
respectively.
After creating the request and response objects it invokes the
[Link](ServletRequest, ServletResponse) method by passing
the request and response objects.
When a Servlet container decides to destroy the Servlet, it
performs the following operations,
◦ It allows all the threads currently running in the service method of the
Servlet instance to complete their jobs and get released.
◦ After currently running threads have completed their jobs, the Servlet
container calls the destroy() method on the Servlet instance.
After the destroy() method is executed, the Servlet container
releases all the references of this Servlet instance so that it
becomes eligible for garbage collection.
There are three life cycle methods of a Servlet :
1. init()
2. service()
3. destroy()
init() method: The [Link]() method is called by the Servlet container to
indicate that this Servlet instance is instantiated successfully and is about to put
into service.
service() method: The service() method of the Servlet is invoked
to inform the Servlet about the client [Link] method
uses ServletRequest object to collect the data requested by the
client.
This method uses ServletResponse object to generate the
output content.
destroy() method: The destroy() method runs only once during
the lifetime of a Servlet and signals the end of the Servlet
instance.