Java Ee Pract
Java Ee Pract
Open Eclipse
Project Name:
CurrencyConverterEJB
Click Finish.
Right-click CurrencyConverterEJB
Bean Name:
CurrencyBean
Bean Type:
Stateless
Click Finish.
package [Link];
import [Link];
@Stateless
Project Name
CurrencyConverterWeb
Click Finish.
Step 5: Add EJB Project to Web Project
Right Click
CurrencyConverterWeb
Properties
Deployment Assembly
Add
Project
Select
CurrencyConverterEJB
Finish
Create
[Link]
inside
src/main/webapp
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Currency Converter</title>
</head>
<body>
<h2>Currency Converter</h2>
<input type="text"
name="usd"
required>
<br><br>
<input type="submit"
value="Convert">
</form>
</body>
</html>
Right Click
src/main/java
New → Servlet
Servlet Name
CurrencyServlet
Step 8: Write [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/CurrencyServlet")
@EJB
CurrencyBean bean;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
[Link]("text/html");
[Link]("usd"));
[Link]("<html><body>");
[Link]("<h2>Currency Converter</h2>");
[Link]("<h3>");
[Link]("</body></html>");
Project Structure
CurrencyConverterEJB
└── src
└── [Link]
└── [Link]
CurrencyConverterWeb
├── src
│ │
│ └── [Link]
│ │
│ └── [Link]
└── src/main/webapp
└── [Link]
[Link]
Sample Input
USD = 10
Click Convert.
Sample Output
Currency Converter
10 USD = 835.0 INR
1. @Stateless
@Stateless
2. @EJB
@EJB
CurrencyBean bean;
[Link]("usd"));
5. Display Result
Viva Questions
Open Eclipse.
File → New → EJB Project
Project Name:
RoomReservationEJB
Click Finish.
Bean Name:
RoomReservationBean
Bean Type:
Stateless
Click Finish.
package [Link];
import [Link];
@Stateless
public class RoomReservationBean {
else {
}
Step 4: Create Dynamic Web Project
Project Name:
RoomReservationWeb
Click Finish.
Right Click
RoomReservationWeb
Properties
Deployment Assembly
Add
Project
Select
RoomReservationEJB
Finish
src/main/webapp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Room Reservation</title>
</head>
<body>
<input type="text"
name="name"
required>
<br><br>
Room Number
<input type="number"
name="roomNo"
required>
<br><br>
<input type="submit"
value="Reserve Room">
</form>
</body>
</html>
Right Click
src/main/java
New → Servlet
Servlet Name
RoomServlet
Click Finish.
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/RoomServlet")
@EJB
RoomReservationBean bean;
HttpServletResponse response)
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("roomNo"));
String result =
[Link](name, roomNo);
[Link]("<html><body>");
[Link]("</body></html>");
}
Step 9: Project Structure
RoomReservationEJB
└── src
└── [Link]
RoomReservationWeb
├── src
│ │
│ └── [Link]
│ │
│ └── [Link]
└── src/main/webapp
│
└── [Link]
[Link]
Output 1
Input
Output
Reservation Successful
Output 2
Input
Output
@Stateless
@Stateless
@EJB
@EJB
RoomReservationBean bean;
[Link]("roomNo"));
Viva Questions
Aim
A user can:
Note: A Stateful Session Bean remembers the cart items for the same
user across multiple requests.
Open Eclipse
Project Name
ShoppingCartEJB
Click Finish.
Bean Name
ShoppingCartBean
Bean Type
Stateful
Click Finish.
Step 3: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Stateful
[Link](item);
return cart;
Project Name
ShoppingCartWeb
Click Finish.
Step 5: Add EJB Project
Right Click
ShoppingCartWeb
Properties
Deployment Assembly
Add
Project
Select
ShoppingCartEJB
Click Finish.
src/main/webapp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Shopping Cart</title>
</head>
<body>
<h2>Shopping Cart</h2>
Product Name
<input type="text"
name="product"
required>
<br><br>
<input type="submit"
value="Add To Cart">
</form>
</body>
</html>
Right Click
src/main/java
New → Servlet
Servlet Name
ShoppingServlet
Step 8: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/ShoppingServlet")
@EJB
ShoppingCartBean cart;
[Link]("text/html");
String product =
[Link]("product");
[Link](product);
[Link]("<html><body>");
[Link]("<h2>Shopping Cart</h2>");
[Link]("<h3>Products in Cart</h3>");
[Link]("<ul>");
[Link]("</ul>");
[Link]("</body></html>");
Project Structure
ShoppingCartEJB
│
└── src
└── [Link]
└── [Link]
ShoppingCartWeb
├── src
│ │
│ └── [Link]
│ │
│ └── [Link]
└── src/main/webapp
└── [Link]
Run the Project
[Link]
Sample Output
First Input
Output
Shopping Cart
Products in Cart
• Laptop
Second Input
Output
Shopping Cart
Products in Cart
• Laptop
• Mouse
Third Input
Output
Shopping Cart
Products in Cart
• Laptop
• Mouse
• Keyboard
The cart keeps all products because the Stateful Session Bean
remembers the user's cart.
@Stateful
Create Cart
Add Product
[Link](item);
Return Cart
Inject EJB
@EJB
ShoppingCartBean cart;
Injects the Stateful Session Bean into the servlet.
Read Product
String product =
[Link]("product");
[Link](product);
Loops through all products and displays them on the web page.
Viva Questions
Answer:
A Stateful Session Bean maintains client-specific state across multiple
requests. It remembers data for the same user.
Q2. What is the difference between Stateful and Stateless Session
Beans?
Stores user-specific
Does not store user-specific data
data
Answer:
Because it remembers the products added by a user during the session,
allowing the shopping cart contents to persist across multiple requests.
Answer:
No. EJB applications require an EJB-compatible application server such
as GlassFish, Payara, or WildFly. Apache Tomcat supports Servlets and
JSP but not full EJB functionality.
8. Implement the following EJB applications with different types of
Beans
Aim
Concept
A Singleton Session Bean has only one instance for the entire
application.
Every time a user accesses the servlet, the hit count increases.
Since there is only one bean instance, all users share the same hit
counter.
Open Eclipse
Project Name
HitCounterEJB
Click Finish.
Bean Name
HitCounterBean
Bean Type
Singleton
Click Finish.
package [Link];
import [Link];
@Singleton
hitCount++;
return hitCount;
}
}
Project Name
HitCounterWeb
Click Finish.
Right Click
HitCounterWeb
Properties
Deployment Assembly
Add
Project
↓
Select
HitCounterEJB
Click Finish.
Right Click
src/main/java
New → Servlet
Servlet Name
HitServlet
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/HitServlet")
@EJB
HitCounterBean bean;
HttpServletResponse response)
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("</body></html>");
Project Structure
HitCounterEJB
└── src
│
└── [Link]
└── [Link]
HitCounterWeb
├── src
│ │
│ └── [Link]
│ │
│ └── [Link]
└── src/main/webapp
[Link]
Output
First Visit
Total Hits : 1
Total Hits : 2
Refresh Again
Total Hits : 3
Every time the servlet is accessed, the hit count increases because the
Singleton Session Bean maintains a single shared counter.
@Singleton
Creates only one instance of the bean for the entire application.
hitCount++;
Increases the counter each time the servlet calls the bean.
@EJB
HitCounterBean bean;
Answer:
A Singleton Session Bean has only one instance for the entire
application. All users share the same bean instance.
Answer:
Because all users should share one common hit counter. A Singleton
Bean allows the application to maintain a single global count.
@Singleton
b. Develop simple visitor Statistics application using Message Driven
Bean [Stateless Session Bean
Aim
Concept
Open Eclipse
Project Name
VisitorStatisticsEJB
Click Finish.
Bean Name
VisitorBean
Bean Type
Stateless
Click Finish.
Step 3: [Link]
package [Link];
import [Link];
@Stateless
count++;
return count;
Bean Name
VisitorMDB
Click Finish.
Step 5: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "[Link]")
})
VisitorBean bean;
@Override
Project Name
VisitorStatisticsWeb
Click Finish.
Step 7: Add EJB Project
Right Click
VisitorStatisticsWeb
Properties
Deployment Assembly
Add → Project
Select
VisitorStatisticsEJB
Finish.
VisitorServlet
Step 9: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/VisitorServlet")
@EJB
VisitorBean bean;
HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
[Link]("<html><body>");
[Link]("<h2>Visitor Statistics</h2>");
[Link]("</body></html>");
}
Project Structure
VisitorStatisticsEJB
└── src
└── [Link]
├── [Link]
└── [Link]
VisitorStatisticsWeb
├── src
│ │
│ └── [Link]
│ │
│ └── [Link]
Run the Project
[Link]
Output
First Visit
Visitor Statistics
Total Visitors : 1
Refresh
Visitor Statistics
Total Visitors : 2
Refresh Again
Visitor Statistics
Total Visitors : 3
1. @Stateless
@Stateless
Creates a Stateless Session Bean that contains the business logic for
counting visitors.
2. @MessageDriven
@MessageDriven
Declares the class as a Message Driven Bean (MDB) that listens for
messages sent to a JMS Queue.
@EJB
VisitorBean bean;
count++;
Increments the visitor count whenever a new visitor is processed.
5. Process Message
Viva Questions
Answer:
A Message Driven Bean is an EJB component that processes messages
asynchronously from a JMS Queue or Topic.
Answer:
It marks a class as a Message Driven Bean that listens for JMS messages.
Answer:
A Stateless Session Bean does not maintain client-specific state. Each
request is handled independently.
Aim
USE college;
name VARCHAR(50),
marks INT
);
Open Eclipse
Project Name
MarksEntryEJB
Click Finish.
Bean Name
MarksBean
Bean Type
Stateless
Click Finish.
Download [Link]
Right Click Project
Build Path
→ Libraries
Select
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Stateless
try {
[Link]("[Link]");
Connection con =
[Link](
"jdbc:mysql://localhost:3306/college",
"root",
"root");
PreparedStatement ps =
[Link](
[Link](1, name);
[Link](2, marks);
[Link]();
[Link]();
} catch(Exception e) {
return [Link]();
Project Name
MarksEntryWeb
Click Finish.
Step 7: Add EJB Project
Right Click
MarksEntryWeb
Properties
Deployment Assembly
Add
Project
Select
MarksEntryEJB
Click Finish.
src/main/webapp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Marks Entry</title>
</head>
<body>
Student Name
<input type="text"
name="name"
required>
<br><br>
Marks
<input type="number"
name="marks"
required>
<br><br>
<input type="submit"
value="Save">
</form>
</body>
</html>
Right Click
src/main/java
↓
New → Servlet
Servlet Name
MarksServlet
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/MarksServlet")
@EJB
MarksBean bean;
HttpServletResponse response)
[Link]("text/html");
[Link]("marks"));
String result =
[Link](name, marks);
[Link]("<html><body>");
[Link]("</body></html>");
Project Structure
MarksEntryEJB
└── src
└── [Link]
└── [Link]
MarksEntryWeb
├── src
│ └── [Link]
│ └── [Link]
└── src/main/webapp
└── [Link]
[Link]
Sample Input
Marks : 85
Click Save.
Output
After executing:
Example result:
id name marks
1 Ali 85
2 Sara 92
@Stateless
@Stateless
[Link]("[Link]");
Database Connection
Connection con =
[Link](
"jdbc:mysql://localhost:3306/college",
"root",
"root");
PreparedStatement
PreparedStatement ps =
[Link](
Set Values
[Link](1, name);
[Link](2, marks);
Execute Query
[Link]();
@EJB
MarksBean bean;
Injects the MarksBean into the servlet so that its saveMarks() method
can be called.
Viva Questions
Answer:
EJB (Enterprise JavaBeans) is a server-side technology used to
implement business logic in enterprise Java applications.
Answer:
It creates a Stateless Session Bean, which does not maintain client-
specific state between requests.
Answer:
It improves performance and helps prevent SQL injection attacks.
Answer:
It executes SQL statements such as INSERT, UPDATE, and DELETE,
returning the number of affected rows.
9. Implement the following JPA applications
Accept Product ID
Accept Quantity
Accept Price
name VARCHAR(50),
quantity INT,
price DOUBLE
);
Open Eclipse
Project Name
InventoryJPA
Click Next.
Click Finish.
Build Path
→ Libraries
Select
[Link]
Create package
[Link]
Create class
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Table(name="product")
@Id
public Product() {
return id;
}
public void setId(int id) {
[Link] = id;
return name;
[Link] = name;
return quantity;
[Link] = quantity;
}
public double getPrice() {
return price;
[Link] = price;
Create
META-INF → [Link]
<persistence version="3.0"
xmlns="[Link]
<persistence-unit name="InventoryPU">
<class>[Link]</class>
<properties>
<property name="[Link]"
value="[Link]"/>
<property name="[Link]"
value="jdbc:mysql://localhost:3306/inventorydb"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="[Link].MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
Create
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
EntityManagerFactory emf =
[Link]("InventoryPU");
EntityManager em = [Link]();
[Link](101);
[Link]("Laptop");
[Link](10);
[Link](55000);
[Link]().begin();
[Link](p);
[Link]().commit();
[Link]();
[Link]();
Project Structure
InventoryJPA
├── src
│ └── [Link]
│ ├── [Link]
│ └── [Link]
└── META-INF
└── [Link]
3. Console Output:
Product Saved Successfully
Database Output
Example Output
@Entity
@Entity
@Table
@Table(name="product")
@Id
@Id
Specifies the primary key of the entity.
Create EntityManagerFactory
EntityManagerFactory emf =
[Link]("InventoryPU");
Create EntityManager
EntityManager em = [Link]();
Begin Transaction
[Link]().begin();
Persist Entity
[Link](p);
Commit Transaction
[Link]().commit();
Viva Questions
Aim
Accept Message
USE guestbookdb;
name VARCHAR(50),
message VARCHAR(200)
);
Open Eclipse
Project Name
GuestbookJPA
Build Path
→ Libraries
Select [Link].
Create package
[Link]
Create class
[Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Table(name="guest")
@Id
public Guest() {
return id;
return name;
[Link] = name;
return message;
[Link] = message;
}
Step 5: Configure [Link]
Create
META-INF
└── [Link]
[Link]
<persistence version="3.0"
xmlns="[Link]
<persistence-unit name="GuestbookPU">
<class>[Link]</class>
<properties>
<property name="[Link]"
value="[Link]"/>
<property name="[Link]"
value="jdbc:mysql://localhost:3306/guestbookdb"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="[Link].MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
Create class
[Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
EntityManagerFactory emf =
[Link]("GuestbookPU");
EntityManager em = [Link]();
[Link](1);
[Link]("Ali");
[Link]("Welcome to our Guestbook!");
[Link]().begin();
[Link](guest);
[Link]().commit();
[Link]();
[Link]();
Project Structure
GuestbookJPA
├── src
│ └── [Link]
│ ├── [Link]
│ └── [Link]
└── META-INF
└── [Link]
Console Output
Database Output
Example Output:
id name message
@Entity
@Entity
@Table
@Table(name="guest")
@Id
@Id
Create EntityManagerFactory
EntityManagerFactory emf =
[Link]("GuestbookPU");
Create EntityManager
EntityManager em = [Link]();
Used to perform database operations.
Begin Transaction
[Link]().begin();
Save Entity
[Link](guest);
Commit Transaction
[Link]().commit();
Viva Questions
Answer: It tells JPA that the class should be treated as a database entity.
Answer: The persist() method inserts a new entity object into the
Store Book ID
USE librarydb;
name VARCHAR(100),
author VARCHAR(100),
price DOUBLE
);
Open Eclipse
Project Name
BookJPA
Click Next
Click Finish.
Download [Link]
Build Path
→ Libraries
Select
[Link]
Create Package
[Link]
Create Class
[Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Table(name="book")
@Id
private int id;
public Book() {
return id;
[Link] = id;
[Link] = name;
return author;
[Link] = author;
return price;
Create
META-INF
└── [Link]
[Link]
<persistence version="3.0"
xmlns="[Link]
<persistence-unit name="BookPU">
<class>[Link]</class>
<properties>
<property name="[Link]"
value="[Link]"/>
<property name="[Link]"
value="jdbc:mysql://localhost:3306/librarydb"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="root"/>
<property name="[Link]"
value="[Link].MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
Create class
[Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
EntityManagerFactory emf =
[Link]("BookPU");
EntityManager em = [Link]();
// Store Book
[Link](101);
[Link]("Java Programming");
[Link]("James Gosling");
[Link](550.00);
[Link]().begin();
[Link](b);
[Link]().commit();
// Retrieve Book
Book book = [Link]([Link], 101);
[Link]("Book Details");
[Link]();
[Link]();
}
Project Structure
BookJPA
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ └── [Link]
└── META-INF
└── [Link]
2. Select
Console Output
Book Saved Successfully
Book Details
ID : 101
Price : 550.0
Database Output
Execute:
Output
@Entity
2. @Table
@Table(name="book")
3. @Id
@Id
4. Create EntityManagerFactory
EntityManagerFactory emf =
[Link]("BookPU");
5. Create EntityManager
EntityManager em = [Link]();
[Link](b);
7. Retrieve Data
8. Commit Transaction
[Link]().commit();
Viva Questions
Answer:
JPA (Java Persistence API) is a Java specification used for Object
Relational Mapping (ORM), allowing Java objects to be stored and
retrieved from a database.
Answer:
@Id identifies the primary key field of the entity.
Answer:
persist() stores a new entity object in the database.
Answer:
find() retrieves an entity from the database using its primary key.
10. Implement the following JPA applications with ORM and Hibernate
Requirements List
1. Software Required
JDK 17 or later
MySQL Server
MySQL Workbench
2. Database
Example:
3. JDBC Driver
[Link]
to the project.
4. JPA Provider
EclipseLink
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
(If you are using Maven, these libraries are downloaded automatically.)
Project Structure
ORMAssociationProject
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
├── META-INF
│ │
│ └── [Link]
├── [Link]
├── [Link]
└── [Link]
1. Entity Class 1
[Link]
2. Entity Class 2
[Link]
3. Main Class
[Link]
4. Configuration File
[Link]
Database Tables
Example (One-to-Many)
Student
id name
1 Ali
Course
id courseName student_id
101 Java 1
102 Python 1
JPA Annotations Required
One-to-One
@OneToOne
One-to-Many
@OneToMany
Many-to-One
@ManyToOne
Many-to-Many
@ManyToMany
Primary Key
@Id
Entity
@Entity
Table
@Table
Join Column
@JoinColumn
@JoinTable
Configuration File
You need
META-INF/[Link]
Database URL
Username
Password
JDBC Driver
Hibernate Dialect
One-to-One
One-to-Many
Many-to-One
Many-to-Many
1. Install JDK.
7. Configure [Link].
Aim
Accept Visitor ID
Accept Email
Accept Feedback
Software Required
Eclipse IDE
JDK 17 or above
MySQL Server
MySQL Workbench
Hibernate ORM
MySQL Connector/J
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
USE feedbackdb;
name VARCHAR(50),
email VARCHAR(100),
message VARCHAR(250)
);
Open Eclipse
Project Name
FeedbackHibernate
Click Finish.
Step 3: Add Hibernate and MySQL JAR Files
Build Path
→ Libraries
[Link]
Create
[Link]
package [Link];
public Feedback() {
return id;
[Link] = id;
return name;
return email;
[Link] = email;
return message;
[Link] = message;
}
Step 6: Create Hibernate Configuration File
Create
[Link]
"[Link]
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">
[Link]
</property>
<property name="[Link]">
jdbc:mysql://localhost:3306/feedbackdb
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
[Link].MySQL8Dialect
</property>
<property name="[Link]">
update
</property>
<property name="show_sql">
true
</property>
<mapping resource="[Link]"/>
</session-factory>
</hibernate-configuration>
Create
[Link]
<?xml version="1.0"?>
"[Link]
<hibernate-mapping>
<class name="[Link]"
table="feedback">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="email"/>
<property name="message"/>
</class>
</hibernate-mapping>
Create
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("[Link]");
SessionFactory factory =
[Link]();
Session session =
[Link]();
Transaction tx =
[Link]();
[Link](1);
[Link]("Ali");
[Link]("ali@[Link]");
[Link]("Website is Excellent");
[Link](f);
[Link]();
[Link]();
[Link]();
}
Project Structure
FeedbackHibernate
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ └── [Link]
├── [Link]
├── [Link]
2. Select
Run As → Java Applica on
Console Output
Database Output
Execute:
Example Output
Create Configuration
Build SessionFactory
SessionFactory factory =
[Link]();
Open Session
Session session =
[Link]();
Begin Transaction
Transaction tx =
[Link]();
Save Object
[Link](f);
Stores the Feedback object in the database.
Commit Transaction
[Link]();
Viva Questions
Aim
Store Employee ID
Store Department
Store Salary
Software Required
Eclipse IDE
JDK 17 or above
MySQL Server
MySQL Workbench
Hibernate ORM
MySQL Connector/J
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
USE employeedb;
CREATE TABLE employee(
name VARCHAR(50),
department VARCHAR(50),
salary DOUBLE
);
Open Eclipse
Project Name
EmployeeHibernate
Click Finish.
Build Path
→ Libraries
[Link]
Create
[Link]
package [Link];
return id;
[Link] = id;
return name;
[Link] = name;
[Link] = department;
return salary;
[Link] = salary;
Create
[Link]
"[Link]
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">
[Link]
</property>
<property name="[Link]">
jdbc:mysql://localhost:3306/employeedb
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
[Link].MySQL8Dialect
</property>
<property name="[Link]">
update
</property>
<property name="show_sql">
true
</property>
<mapping resource="[Link]"/>
</session-factory>
</hibernate-configuration>
Create
[Link]
<?xml version="1.0"?>
"[Link]
<hibernate-mapping>
<class name="[Link]"
table="employee">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="department"/>
<property name="salary"/>
</class>
</hibernate-mapping>
Create
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmployeeApp {
[Link]("[Link]");
SessionFactory factory =
[Link]();
Session session =
[Link]();
// Store Employee
Transaction tx =
[Link]();
Employee emp = new Employee();
[Link](101);
[Link]("Rahul");
[Link]("IT");
[Link](50000);
[Link](emp);
[Link]();
// Retrieve Employee
[Link]("\nEmployee Details");
[Link]();
[Link]();
Project Structure
EmployeeHibernate
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ └── [Link]
│
├── [Link]
├── [Link]
2. Select
Console Output
Employee Details
ID : 101
Name : Rahul
Department : IT
Salary : 50000.0
Database Output
Execute:
Example Output
Create Configuration
[Link]("[Link]");
Build SessionFactory
SessionFactory factory =
[Link]();
Open Session
Session session =
[Link]();
Begin Transaction
Transaction tx =
[Link]();
Save Employee
[Link](emp);
Retrieve Employee
Employee e =
[Link]([Link], 101);
Commit Transaction
[Link]();
Viva Questions
Answer:
Hibernate is an ORM (Object Relational Mapping) framework that maps
Java objects to database tables.
Answer:
ORM (Object Relational Mapping) is a technique that maps Java classes
to database tables and Java objects to database records.
Answer:
SessionFactory creates Session objects that interact with the database.
Answer:
[Link]() inserts a Java object as a new record into the database.
Q5. What is the purpose of [Link]()?
Answer:
[Link]() retrieves a record from the database using its primary key
and returns it as a Java object.
Aim
Develop a Hibernate Application to demonstrate One-to-One Mapping
using Annotations.
Example:
Software Required
Eclipse IDE
JDK 17 or above
MySQL Server
MySQL Workbench
Hibernate ORM
MySQL Connector/J
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
USE hibernatedb;
Project Name
HibernateOneToOne
Click Finish.
Build Path
→ Configure Build Path
→ Libraries
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
@OneToOne(cascade = [Link])
@JoinColumn(name="address_id")
public Employee() {
return id;
[Link]=id;
}
return name;
[Link]=name;
return address;
[Link]=address;
}
Step 6: Create [Link]
package [Link];
import [Link];
import [Link];
@Entity
@Id
public Address() {
[Link]=id;
return city;
[Link]=city;
return state;
"[Link]
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">
[Link]
</property>
<property name="[Link]">
jdbc:mysql://localhost:3306/hibernatedb
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
[Link].MySQL8Dialect
</property>
<property name="[Link]">
update
</property>
<property name="show_sql">
true
</property>
<mapping class="[Link]"/>
<mapping class="[Link]"/>
</session-factory>
</hibernate-configuration>
Create
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Configuration cfg =
new Configuration().configure();
SessionFactory factory =
[Link]();
Session session =
[Link]();
Transaction tx =
[Link]();
Address address =
new Address();
[Link](1);
[Link]("Mumbai");
[Link]("Maharashtra");
Employee emp =
new Employee();
[Link](101);
[Link]("Rahul");
[Link](address);
[Link](emp);
[Link]();
[Link]();
[Link]();
[Link]("Employee and Address Saved Successfully");
Project Structure
HibernateOneToOne
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
├── [Link]
2. Select
Console Output
Hibernate:
Hibernate:
Database Tables
Employee Table
id name address_id
101 Rahul 1
Address Table
id city state
1 Mumbai Maharashtra
@Entity
@Entity
@Id
@Id
@OneToOne
@OneToOne
@JoinColumn
@JoinColumn(name="address_id")
cascade = [Link]
Viva Questions
Answer:
One-to-One Mapping means one record in one table is associated with
exactly one record in another table.
Answer:
@OneToOne
Answer:
It creates a foreign key column that links two tables.
Answer:
It tells Hibernate that the Java class should be mapped to a database
table.
Aim
Store Course ID
Store Duration
Store Fees
Retrieve and display the course details.
Software Required
Eclipse IDE
JDK 17 or above
MySQL Server
MySQL Workbench
Hibernate ORM
MySQL Connector/J
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
USE coursedb;
name VARCHAR(50),
duration VARCHAR(30),
fees DOUBLE
);
Open Eclipse
Project Name
CourseHibernate
Click Finish.
Build Path
→ Libraries
Add:
[Link]
package [Link];
public Course() {
return id;
[Link] = id;
return name;
[Link] = name;
}
return duration;
[Link] = duration;
return fees;
[Link] = fees;
<?xml version="1.0"?>
"[Link]
<hibernate-mapping>
<class name="[Link]"
table="course">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="duration"/>
<property name="fees"/>
</class>
</hibernate-mapping>
"[Link]
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">
[Link]
</property>
<property name="[Link]">
jdbc:mysql://localhost:3306/coursedb
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
root
</property>
<property name="[Link]">
[Link].MySQL8Dialect
</property>
<property name="[Link]">
update
</property>
<property name="show_sql">
true
</property>
<mapping resource="[Link]"/>
</session-factory>
</hibernate-configuration>
Create [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class CourseApp {
[Link]("[Link]");
SessionFactory factory =
[Link]();
Session session =
[Link]();
// Store Course
Transaction tx =
[Link]();
[Link]("Java Programming");
[Link]("3 Months");
[Link](15000);
[Link](c);
[Link]();
// Retrieve Course
Course course =
[Link]([Link], 101);
[Link]("\nCourse Details");
[Link]();
[Link]();
Project Structure
CourseHibernate
├── src
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ └── [Link]
│
├── [Link]
├── [Link]
2. Select
Console Output
Course Details
Course ID : 101
Fees : 15000.0
Database Output
Execute:
Output:
1. Create Configuration
[Link]("[Link]");
3. Build SessionFactory
SessionFactory factory =
[Link]();
4. Open Session
Session session =
[Link]();
5. Begin Transaction
Transaction tx =
[Link]();
6. Save Course
[Link](c);
7. Retrieve Course
Course course =
[Link]([Link], 101);
8. Commit Transaction
[Link]();
Viva Questions
Answer: It retrieves an object from the database using its primary key.
c. Develop a five page web application site using any two or three Java
EE Technologies
Aim
Technologies Used
HTML
CSS
Servlet
JDBC
MySQL
1. Servlet
2. JDBC
3. MySQL Database
Software Required
JDK 17 or above
MySQL Server
MySQL Workbench
MySQL Connector/J
Project Name
StudentPortal
Project Pages
Project Structure
StudentPortal
├── src
│ └── [Link]
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
├── WebContent
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
└── [Link]
Database
Create Database
USE studentdb;
username VARCHAR(50),
password VARCHAR(50),
email VARCHAR(50)
);
Page 1 : [Link]
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Student Portal</h1>
<a href="[Link]">Register</a>
<br><br>
<a href="[Link]">Login</a>
<br><br>
<a href="[Link]">About</a>
</body>
</html>
Page 2 : [Link]
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Registration Form</h2>
Username
<input type="text"
name="username"
required>
<br><br>
Password
<input type="password"
name="password"
required>
<br><br>
<input type="email"
name="email"
required>
<br><br>
<input type="submit"
value="Register">
</form>
</body>
</html>
Page 3 : [Link]
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="LoginServlet" method="post">
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>
Page 4 : [Link]
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h2>About Website</h2>
<p>
Servlet,
JDBC
and
MySQL.
</p>
<a href="[Link]">
Home
</a>
</body>
</html>
[Link]
package [Link];
import [Link];
import [Link];
try {
[Link]("[Link]");
con = [Link](
"jdbc:mysql://localhost:3306/studentdb",
"root",
"root");
catch(Exception e) {
[Link]();
return con;
}
[Link]
package [Link];
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet("/RegisterServlet")
HttpServletResponse response)
String username=[Link]("username");
String password=[Link]("password");
String email=[Link]("email");
try{
Connection con=[Link]();
PreparedStatement ps=[Link](
[Link](1,username);
[Link](2,password);
[Link](3,email);
[Link]();
[Link]("[Link]");
catch(Exception e){
[Link]();
[Link]
package [Link];
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet("/LoginServlet")
HttpServletResponse response)
[Link]("text/html");
PrintWriter out=[Link]();
String username=[Link]("username");
String password=[Link]("password");
try{
Connection con=[Link]();
PreparedStatement ps=[Link](
[Link](1,username);
[Link](2,password);
ResultSet rs=[Link]();
if([Link]()){
[Link]("username",username);
RequestDispatcher
rd=[Link]("WelcomeServlet");
[Link](request,response);
else{
[Link]("<h2>Login Failed</h2>");
catch(Exception e){
[Link]();
}
}
[Link]
package [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet("/WelcomeServlet")
HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out=[Link]();
String user=(String)
[Link]("username");
[Link]("<h1>Welcome "+user+"</h1>");
[Link]("<a href='[Link]'>About</a>");
5. Click Finish.
6. Open:
[Link]
Project Flow
Home Page
▼
Registration Page
▼
Data Stored in MySQL
▼
Login Page
│
▼
LoginServlet
▼
WelcomeServlet
▼
About Page
Output
Home Page
Student Portal
Register
Login
About
Registration
Username : Rahul
Password : 12345
Email : rahul@[Link]
Login
Username : Rahul
Password : 12345
Welcome Page
Welcome Rahul
About
Viva Questions
Aim
Spring Boot 3
Spring MVC
Embedded Tomcat
HTML View
Software Required
JDK 17 or above
Spring Boot 3
Maven
Web Browser
Dependencies Required
While creating the project, select:
Spring Web
Thymeleaf
Open Eclipse
Project Name
SpringBootWebApp
Group
[Link]
Artifact
SpringBootWebApp
Packaging
Jar
Java Version
17
Click Next.
Select Dependencies
Spring Web
Thymeleaf
Click Finish.
Project Structure
SpringBootWebApp
├── src/main/java
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ └── [Link]
├── src/main/resources
│ │
│ ├── static
│ │
│ ├── templates
│ │ └── [Link]
│ │
│ └── applica [Link] es
└── [Link]
Create
[Link]
package [Link];
import [Link];
import [Link];
@SpringBootApplication
[Link](
[Link],
args);
}
Create
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Controller
@GetMapping("/")
return "index";
Create
src/main/resources/templates/[Link]
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<meta charset="UTF-8">
<title>Spring Boot</title>
</head>
<body>
<h1 th:text="${message}"></h1>
<p>
</p>
</body>
</html>
Step 5: [Link]
Create
src/main/resources/[Link]
[Link]=SpringBootWebApp
[Link]=8080
Step 6: [Link]
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2. Select
3. Open Browser
[Link]
Output
@SpringBootApplication
@SpringBootApplication
[Link]()
[Link](
[Link],
args);
Starts the Spring Boot application and the embedded Tomcat server.
@Controller
@Controller
@GetMapping("/")
@GetMapping("/")
Model
[Link]("message",
"Welcome to Spring Boot 3");
th:text
<h1 th:text="${message}"></h1>
Viva Questions
Answer:
Spring Boot is a framework built on the Spring Framework that
simplifies the development of Java applications by providing auto-
configuration, embedded servers, and production-ready features.
Answer:
It combines @Configuration, @EnableAutoConfiguration, and
@ComponentScan to configure and start the application automatically.
Answer:
Thymeleaf is a server-side Java template engine used to create dynamic
HTML pages in Spring Boot applications.
Answer:
No. Spring Boot includes an embedded Tomcat server by default, so
you can run the application directly without installing or configuring
Tomcat separately.
b. Develop application using Spring Framework, Lightweight
Aim
Software Required
Eclipse IDE
JDK 17 or above
Maven (Recommended)
Technologies Used
Spring Core
Spring Context
Java
Open Eclipse
Project Name
SpringLightweightApp
Group Id
[Link]
Artifact Id
SpringLightweightApp
Click Finish.
Open [Link]
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.8</version>
</dependency>
</dependencies>
Save the file. Maven will automatically download the required libraries.
SpringLightweightApp
├── src/main/java
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
│
└── [Link]
package [Link];
package [Link];
import [Link];
import [Link];
@Configuration
@Bean
package [Link];
import [Link];
import
[Link]
ontext;
public class MainApp {
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
[Link]();
Project Structure
SpringLightweightApp
├── src/main/java
│ │
│ └── [Link]
│ │
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
└── [Link]
1. Right-click [Link]
Console Output
@Configuration
@Configuration
@Bean
@Bean
ApplicationContext
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
getBean()
[Link]();
Easy to maintain
Viva Questions
Answer:
Spring Framework is a lightweight Java framework used to develop
enterprise applications. It provides features like Dependency Injection
(DI) and Inversion of Control (IoC).
Answer:
IoC means the Spring container creates and manages objects instead of
the programmer creating them using the new keyword.
Answer:
Dependency Injection is a technique where Spring automatically
provides the required objects (dependencies) to a class.
Answer:
ApplicationContext is the Spring container that creates, configures, and
manages Spring beans.
Answer:
A Bean is a Java object that is created, configured, and managed by the
Spring container.
c. Containers and Dependency Injection with Spring
Aim
Software Required
Eclipse IDE
JDK 17 or above
Maven
Technologies Used
Spring Core
Java
Step 1: Create Maven Project
Open Eclipse
Project Name
SpringCDIApp
Group Id
[Link]
Artifact Id
SpringCDIApp
Click Finish.
Open [Link]
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.8</version>
</dependency>
</dependencies>
SpringCDIApp
├── src/main/java
│ └── [Link]
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
└── [Link]
public Address() {
city = "Mumbai";
return city;
package [Link];
import [Link];
public class Student {
@Autowired
+ [Link]());
package [Link];
import [Link];
import [Link];
import [Link];
@Configuration
@ComponentScan(basePackages="[Link]")
@Bean
@Bean
package [Link];
import [Link];
import
[Link]
nContext;
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
Student student =
[Link]([Link]);
[Link]();
Project Structure
SpringCDIApp
├── src/main/java
│ └── [Link]
│ │
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
│
└── [Link]
1. Right-click [Link]
Console Output
@Configuration
@Configuration
@Bean
@Bean
}
Creates a Spring-managed bean.
@Autowired
@Autowired
ApplicationContext
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
getBean()
Example:
Advantages
Viva Questions