J2EE Unit2 31-07-25
J2EE Unit2 31-07-25
J2EE and Web Development: J2EE Architecture Types, J2EE Containers, Types of Servers in J2EE
Application, HTTP Protocols and API, Request Processing in Web Application, Web Application Structure,
Web Containers and Web Architecture Models.
Definition of J2EE: J2EE (Java 2 Platform, Enterprise Edition) is a Java-based platform used to build
large and powerful applications like banking systems, online shopping sites, and booking systems. OR
Key Points:
It is platform-independent, which means the applications made using J2EE can run on any operating
system that supports Java.
It is called Java-centric because it mainly uses the Java programming language and tools.
J2EE was originally developed by Sun Microsystems, and now it is maintained by Oracle.
It includes rules and tools (called APIs and specifications) that help developers build reliable,
scalable, and secure applications.
Purpose of J2EE:
J2EE helps developers easily build large web and distributed applications by providing built-in services,
reusable components, and APIs such as Servlets, JSP, EJB, and JDBC. It is used to develop applications like
shopping websites, banking systems, and online booking platforms using the Java programming language.
These are ready-made features provided by J2EE to handle common tasks automatically, like:
Example: In a banking app, you don’t have to write code for handling secure transactions — J2EE provides it
for you.
2. Reusable Components : These are pre-written pieces of code (components) that can be used in many
applications without rewriting them.
Example: Enterprise JavaBeans (EJB) – You can write one EJB to manage customer data and reuse it in
different parts of your banking or shopping system.
3. APIs (Application Programming Interfaces) : APIs are predefined libraries that let you connect to
databases, create web pages, or send messages.
Example:
Main Purpose:
JSP (Java Server Pages) Show dynamic web pages (e.g., product list)
EJB (Enterprise JavaBeans) Write business logic (e.g., place order, calculate total)
A web application is a software program that runs on a web server and is accessed by users through a web
browser over the Internet or an intranet. Unlike desktop applications, web applications don’t need to be
installed on the user's machine. They are platform-independent and can be accessed from anywhere using a
browser. Example:
Gmail is a web application that lets you send and receive emails directly from your browser without
installing any software.
1. User Login:
o You open https://mail.google.com in a web browser.
o A login page is shown. You enter your email and password.
2. Request Sent to Web Server:
o Your credentials are sent to Google's web server using a secure HTTPS request.
3. Authentication:
o The server checks your credentials in the backend.
o If correct, it allows access; otherwise, it returns an error.
4. Fetching Emails:
o The server retrieves your emails from the database and sends them back as HTML and JSON
data.
5. User Interface Display:
o Your browser renders the Gmail interface (inbox, compose, folders).
o You can read, send, delete, or archive emails—all done through web interactions.
6. No Installation Needed:
o No need to install software; just use a browser on any device.
A distributed application is a software program that runs on multiple computers or servers connected
through a network (like the Internet). These computers work together to perform different tasks, but
to the user, it looks like a single application.
Let’s say you buy a mobile phone on Flipkart and pay using PayPal.
What happens:
Key Points:
A payment gateway is a technology or service that allows websites or apps to accept online payments
(like credit cards, debit cards, UPI, net banking, etc.).
Razorpay
Stripe
PayU
CCAvenue
PhonePe Payment Gateway
Amazon Pay Gateway
What is PayPal?
1. A payment gateway
2. A digital wallet
3. A merchant account
Send/receive money
Store their card/bank details
Shop online
Integrate PayPal into their websites (like a gateway
J2EE Architecture Types :J2EE supports four main architecture models:
1.Single tier Architecture
1. Single-Tier Architecture: A 1-tier architecture is the simplest type of application architecture where
all components—user interface, business logic, and data access—reside in a single layer or program.
Example Technologies:
Example Scenario:
Advantages:
Explanation:
In 2-tier architecture:
The client application (like a Java desktop app) handles the user interface and part of the business
logic.
The database server (like MySQL, Oracle DB) handles data storage.
The client connects to the database using JDBC (Java Database Connectivity) or similar
technologies.
Example Technologies:
Example Scenario:
The client app has forms for adding books, viewing members, issuing books, etc.
It connects directly to the database using JDBC.
All the data (books, members, transactions) is stored in a MySQL database.
There is a direct connection between client and database, without any middle layer.
Advantages:
Disadvantages:
3. Three-Tier Architecture:
Definition: A 3-tier architecture divides an application into three separate layers, each with its own role.
This separation improves scalability, maintainability, and security.
2. Business Logic Tier (Application/Service Layer): The Business Logic Tier (or middle layer) processes
the data sent by the presentation layer. It contains the core functionality of the application and is where all
the business rules are implemented. In Java-based applications, this layer typically uses Servlets,
Enterprise JavaBeans (EJB), or frameworks like spring. This tier acts as a bridge between the user
interface and the data layer, performing operations such as validation, processing user requests, and
managing application workflows.
3. Data Tier (Database Layer): The Data Tier (or backend layer) is responsible for storing, retrieving, and
managing the data used by the application. It interacts directly with the database using technologies like
JDBC, JPA, or Hibernate. This layer is isolated from the user and only communicates with the business
layer, ensuring better security and data integrity.
In a typical 3-tier Java application, when a user performs an action (like logging in), the request is sent from
the presentation layer to the business logic layer, which processes the request and interacts with the data layer
to verify credentials. The result is then sent back up through the business layer to the presentation layer for
display.
Structure:
[Browser]
↓
[Web Server (Servlets/JSP)]
↓
[Application Server (Business Logic)]
↓
[Database]
Client Tier: A customer logs into their bank account via a web page (HTML + JSP).
Business Tier: A servlet or EJB handles the login request, validates credentials.
Data Tier: The app queries the user table in the database using JDBC or JPA.
Separation of concerns – Each layer has its own job (UI, logic, data), which makes the system easier
to understand and manage.
Scalable – Add more servers to each layer as needed.
Secure – Client never directly accesses the database (The user can’t directly access the database; all
data goes through the business layer, which helps protect sensitive information.)
Reusable components – Logic can be reused by multiple clients (e.g., web, mobile).( The business
logic layer can be reused by different user interfaces, like a web app and a mobile app)
1. Increased Complexity
o The design is more complicated than 1-tier or 2-tier, which can make it harder to build for
small applications.
2. Slower Performance (sometimes)
o Because data has to pass through more layers, it may be slightly slower, especially for small
tasks.
3. Higher Cost and Resources
o More layers mean more code, more servers, and more setup, which can be expensive and
resource-intensive.
4. More Setup and Configuration
o Requires careful setup of communication between the layers, which can take more
development time.
4. N-Tier Architecture (Multi-Tier Architecture): It is Like 3-tier, but with more than 3 layers.
Definition: -Tier architecture is an extension of the 3-tier architecture where the application is
divided into more than three layers (or tiers). The "N" simply means any number of layers — not
just 3. This design helps organize complex applications into multiple specialized layers, each
handling a specific responsibility.
In J2EE, a web architecture model explains how the client and server work together to process a request
and give a response.
It follows a multi-tier (layered) approach so that each layer does its own job.
4. Enterprise Information System (EIS) Tier: This tier comprises database servers, enterprise resource
planning systems, and other data sources. It provides connectivity to resources that are not part of J2EE, such
as legacy systems and third-party systems
Graphical Representation of J2EE Architecture
Client Tier:
Acts as front end for an application
Acts as a user interface for applications
Get the Input from the user and then converts into requests that are forwarded to the middle
tier(Server).
It translates the server ‘s response into text and presented to the user.
A client can be a web browser, standalone application, or applets
Maintainability: Facilitates easier maintenance and updates by allowing changes to be made to specific parts
of the application without affecting the entire system
[Client]
Container in J2EE:
Container definition): A container in Java is a runtime environment that manages the execution,
lifecycle, and behavior of Java web components like Servlets, JSPs, and EJBs.
Lifecycle Management – Creating and destroying components like Servlets, JSPs, and EJBs.
Security Management – Handling authentication and authorization(Provide login, access control,
and data protection).
Transaction Management – Managing transactions for EJB components.
Resource Management – Managing database connections, thread pools, etc.
Communication Support – For distributed
components (like EJB) i.e Help components
talk to each other using tools like JMS and
JDBC.
Acts as a middle layer between Java code
and the J2EE server.
How It Works – Simple Flow
They:
J2EE defines four main types of containers, each for specific component types:
1.Web Container: (also called a Servlet Container) : is a part of a J2EE application server that:
Example:
When a user sends a login form, the Web Container passes the HTTP request to a LoginServlet, which then
processes the request and sends back a response.
2. EJB Container (Enterprise JavaBeans Container): is a part of the J2EE application server that
manages Enterprise JavaBeans (EJBs). It provides a powerful environment to develop and run business
logic components in a secure, scalable, and transactional way.
Example:
An EJB named AccountBean could contain logic to transfer funds between accounts. The EJB Container
manages the transaction and rolls back if any issue occurs.
3.Application Client Container:
Responsibilities:
Runs Java SE applications that call J2EE services (like EJBs or web services) i.e Enables
clients to access J2EE services (JNDI, JMS, EJB, etc.)
Example: A Java desktop application using Swing can connect to the server through the application client
container to retrieve or update data.
Summary Table:
1.Web Server :
Purpose: To handle HTTP requests and serve static content like HTML, CSS, and images.
Supports: Web technologies like Servlets and JSP (JavaServer Pages) through the Web Container.
Web Container + HTTP request handling = WebServer
Role in J2EE:
Processes client requests and passes dynamic requests
to the Web Container.
Can be integrated with the Application Server or
work as a separate front-end.
➤ Simple Example: When you type www.google.com, the
web server sends you the homepage.
➤ Examples:
➤ Simple Example:Online shopping logic like calculating total bill, applying discount, managing orders—this is
done by the application server.
3.Database Server:
Role in J2EE:
Works with the application server to perform CRUD operations (Create, Read, Update, Delete).
The application connects via JDBC or JPA.
Examples:
MySQL
PostgreSQL
Oracle DB
Microsoft SQL Server
➤ What it does:
➤ Simple Example:When you log in, your username and password are checked from the database server.
➤ Examples:
MySQL
Oracle
PostgreSQL
MS SQL Server
4.Mail Server :
Examples:
➤ What it does:
➤ Simple Example: After you sign up on a website, if you get a confirmation email, that’s handled by the mail
server.
➤ Examples:
Gmail SMTP
Sendmail
Microsoft Exchange
5.FTP Server
Examples:
FileZilla Server
vsftpd
ProFTPD
➤ What it does:
Lets you upload and download files between systems using FTP (File Transfer Protocol).
➤ Simple Example: If your app allows users to upload documents or download reports, FTP can be used.
6. Proxy Server:
Purpose: Distributes requests among multiple servers to ensure load balancing and fault tolerance.
Role in J2EE:
o Improves performance and scalability in large enterprise applications.
Examples:
➤ What it does:
➤ Simple Example: Like a watchman who checks your ID before you enter the building (server).
7.Authentication Server
➤ What it does:
Verifies user login details.
Provides security by allowing only authorized users.
➤ Simple Example: When you log in to your college portal, it checks whether your ID and password are
correct.
➤ Examples:
LDAP Server
Keycloak
Microsoft Active Directory
JEE Server : A JEE server is a run time infrastructure for hosting and managing JEE applications. JEE
server is having the implementation of JEE specification set by SUN/Oracle. All JEE server vendors do this
implementation. Instance of a class is called as objects; same like this instance of servlet, JSP, EJB are called
as servlet component, JSP component and EJB component respectively. Objects are executed inside JVM,
JEE components are executed inside an infrastructure know as containers. Ideally these containers use JVM
itself to execute servlet and JSP components and Servlet and JSP components are also java objects. However
in JEE, component and containers terminologies are used, this is because, container does more tasks than a
JVM does. Components are more sophisticated than just an object.
Request-Response Protocol: Every time you click a link, a request is sent and a response comes
back.
Uses TCP Port 80: It runs over the TCP/IP network, using port 80 by default.
Stateless Protocol: The server does not remember your previous requests. Each request is treated as
new.
Used in Web Apps: Search engines, browsers, and servers like Servlets use HTTP to share
information.
How HTTP Works in J2EE:
HTTP requests are sent by clients (like browsers or mobile apps) to access web pages or call APIs.
Servlets and JSPs handle these requests in a Web Container (like Apache Tomcat).
The Web Container reads the request, processes it, and returns an HTTP response.
GET Retrieve data from the server doGet() View a web page or search result
POST Send data to the server doPost() Submit data to the server
Note : HTTP is the language that lets your browser and websites talk to each other and exchange data
across the internet.
Two common methods for the request-response between a server and client are:
The query string (name/value pairs) is sent inside the URL of a GET request:
1. GET/RegisterDao.jsp?name1=value1&name2=value2
As we know that data is sent in request header in case of get request. It is the default request type. Let's see
what information is sent to the server.
Anatomy of Post Request :The query string (name/value pairs) is sent in HTTP message body for a POST
request:
1. POST/RegisterDao.jsp HTTP/1.1
2. Host: www. javatpoint.com
3. name1=value1&name2=value2
As we know, in case of post request original data is sent in message body. Let's see how information is
passed to the server in case of post request.
2) Get request is not secured because data is exposed in Post request is secured because data is not exposed in
URL bar. URL bar.
5) Get request is more efficient and used more than Post request is less efficient and used less than get.
Post.
An API is a set of functions and protocols that allows one software application to interact with
another.
In J2EE, we create Web APIs using Servlets or RESTful services to let other applications
communicate with ours.
An API (Application Programming Interface) using HTTP helps apps communicate over the
web.
Example: A weather app sending an HTTP GET request to fetch temperature data.
Used for Sending/receiving requests and responses Accessing business logic or resources
Works with Web Container, Servlets, JSPs Web Container, Servlets, JSPs
Request Types GET, POST, PUT, DELETE Exposed as RESTful or SOAP-based endpoints
@WebServlet("/products")
public class ProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.println("{ \"id\": 1, \"name\": \"Mobile\", \"price\": 12000 }");
}
}
The servlet responds to an HTTP GET request with a JSON API response.
That’s HTTP + API working together in a J2EE environment.
API Purpose
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Output (Browser) : When the user enters the name "Aruna" and submits:
URL : http://localhost:8080/YourApp/welcome?username=Aruna
Hello, Aruna!
This response was generated using a servlet.
Summary
A Web Application in J2EE is a software application that runs on a web server and follows a specific
directory structure defined by the Java EE specifications. It can include:
It is usually packaged as a WAR (Web Application Archive) file and deployed on a Web Container (like
Apache Tomcat, GlassFish, etc.).
│ └── com/myapp/
│ └── LoginServlet.class
└── lib/ ← JAR libraries used in the app (JDBC, logging, etc.)
It contains HTML, JSP files, and static resources like CSS, JS, and images.
4.WEB-INF Directory
It contains:
web.xml:
Example:
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.myapp.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
b. classes/
c. lib/
Contains all the JAR files your web app needs (external libraries).
For example, if you're connecting to a MySQL database, you might add:
o mysql-connector-java-8.x.x.jar here.
You can also add your own JAR files here.
Web apps are made of front-end (client), back-end (server), and database.
Java web apps follow a clear folder structure with a special WEB-INF directory.
The Servlet container (like Tomcat) helps in managing servlets and handling requests.
The web.xml file is the deployment descriptor, used to map URLs to servlets.
LoginApp/
├── index.html ← Login form (HTML page)
├── WEB-INF/
│ ├── web.xml ← Deployment descriptor
│ ├── classes/
│ │ └── com/login/
│ │ └── LoginServlet.class ← Compiled servlet
│ └── lib/ ← (empty or JDBC JARs if needed)
2. HTML File (index.html) : Place this in the root of your project folder (LoginApp/index.html):
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>User Login</h2>
<form method="post" action="login">
Username: <input type="text" name="username" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
package com.login;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
LoginApp/WEB-INF/classes/com/login/LoginServlet.class
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
5. How to Run It
Welcome, admin!
Login successful.
3.If credentials are wrong
Login Failed
Invalid username or password.
Web Container :
Definition:
A Web container (also called a Servlet container) is the part of a Java EE / Jakarta EE server that manages
web components — specifically Servlets,
JSPs, and related filters/listeners.
Main Responsibilities
1. Lifecycle Management
o Calls init(), service(), destroy() for Servlets
o Handles JSP compilation and execution
2. Request Handling
o Accepts HTTP/HTTPS requests from the client (browser)
o Routes them to the right Servlet or JSP
o Sends back the HTTP response
3. Session Management
o Tracks users across multiple requests (via HttpSession)
4. Security
o Manages authentication, authorization, and secure connections (SSL/TLS)
SSL and TLS are cryptographic protocols used to secure communication over a network (like
the internet).They:
Encrypt data so no one can read it while it’s being sent.
Authenticate the server (and sometimes the client) to ensure you’re talking to the right party.
Maintain data integrity so it’s not altered during transmission.
5. Deployment Support
o Loads .war files
o Reads web.xml or annotations to configure routes and filters
[ Think of the Web container as the restaurant kitchen for your Java web app — it takes incoming orders
(HTTP requests), cooks them according to the recipe (Servlet/JSP logic), and serves the meal (HTTP
response) to the customer (browser). ]