Jakarta EE Project Configuration ποΈ
The Lunchbox Analogy π±
Imagine youβre packing a lunchbox for school. You need:
- The right container (packaging)
- A label saying whatβs inside (coordinates)
- Instructions for the cafeteria staff (descriptors)
- Stickers that tell everyone special things about your lunch (annotations)
Jakarta EE projects work the same way! Letβs explore each part.
1. Jakarta EE Packaging π¦
What is Packaging?
When you finish building with LEGO, you put it in a box to carry it somewhere. In Jakarta EE, packaging means putting your code in a special container so it can run on a server.
The Three Lunchbox Types
graph TD A["Your Code"] --> B{What type?} B --> C["WAR - Web App"] B --> D["JAR - Java Library"] B --> E["EAR - Everything Together"]
| Package | Full Name | Whatβs Inside | Real Example |
|---|---|---|---|
| WAR | Web Application Archive | Websites, APIs | myshop.war |
| JAR | Java Archive | Reusable code | utils.jar |
| EAR | Enterprise Archive | WAR + JAR together | bigapp.ear |
Simple Example
Think of a pizza shop:
- JAR = Recipe book (reusable recipes)
- WAR = The actual pizza shop (serves customers)
- EAR = The whole franchise (shop + recipes + everything)
2. Maven Coordinates π
What are Maven Coordinates?
Imagine every house has an address. Maven coordinates are like addresses for code libraries.
When you want to use someone elseβs code, you tell Maven exactly where to find it using three things:
groupId β Who made it (like a company name)
artifactId β What it's called (the project name)
version β Which version (like v1.0, v2.0)
Real Example
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
</dependency>
Reading this address:
- π Made by:
jakarta.platform - π Name:
jakarta.jakartaee-api - π Version:
10.0.0
Your Projectβs Own Address
Your project also needs an address! In pom.xml:
<groupId>com.mycompany</groupId>
<artifactId>my-cool-app</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
3. Deployment Descriptors π
What is a Deployment Descriptor?
Remember the instructions you give a babysitter? βFeed the cat at 6pm. No candy before dinner.β
Deployment descriptors are instructions for the server. They tell the server how to run your app.
The Main Descriptor: web.xml
This file lives in src/main/webapp/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
version="6.0">
<display-name>My Cool App</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
What Can You Configure?
| Setting | What It Does | Example |
|---|---|---|
welcome-file |
First page to show | index.html |
session-timeout |
How long to remember users | 30 minutes |
error-page |
What to show when errors happen | error.html |
Good News! π
Modern Jakarta EE apps often donβt need web.xml at all! Annotations (stickers) can do the same job. More on that next!
4. Jakarta EE Annotations β¨
What are Annotations?
Annotations are like magic stickers you put on your code. They tell the server: βHey! This code is special. Treat it this way!β
Why Annotations are Amazing
Before annotations (the old way):
- Write your code
- Write XML configuration
- Make sure they match
- Fix mistakes
- Cry a little
With annotations (the new way):
- Write your code with stickers
- Done! π
Common Jakarta EE Annotations
graph TD A["Jakarta EE Annotations"] --> B["@WebServlet"] A --> C["@Entity"] A --> D["@EJB"] A --> E["@Inject"] B --> F["Handle web requests"] C --> G["Database tables"] D --> H["Business logic"] E --> I["Connect things"]
5. Annotations Basics π·οΈ
How to Write an Annotation
All annotations start with the @ symbol:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
// Your code here
}
The @WebServlet("/hello") sticker tells the server:
- βThis is a web page handler!β
- βShow it when someone visits
/helloβ
Anatomy of an Annotation
@AnnotationName(parameter = "value")
Parts:
@β The magic symbolAnnotationNameβ What kind of sticker(...)β Extra details (optional)
Examples Youβll Use Often
1. Making a Web Page:
@WebServlet("/products")
public class ProductServlet
extends HttpServlet {
// Shows product list
}
2. Saving to Database:
@Entity
@Table(name = "customers")
public class Customer {
@Id
private Long id;
private String name;
}
3. Getting Help from Other Code:
@Inject
private CustomerService service;
Annotations vs XML: Quick Compare
| Feature | Annotations | XML |
|---|---|---|
| Location | Inside your code | Separate file |
| Easy to read | β Very easy | β Can be messy |
| Change without recompile | β No | β Yes |
| Modern Jakarta EE | β Preferred | β οΈ Still works |
Putting It All Together π§©
Letβs see a complete mini-project structure:
my-app/
βββ pom.xml β Maven coordinates
βββ src/
β βββ main/
β βββ java/
β β βββ com/myapp/
β β βββ HelloServlet.java β Has @WebServlet
β βββ webapp/
β βββ WEB-INF/
β βββ web.xml β Deployment descriptor
βββ target/
βββ my-app.war β Final package!
The Flow
graph TD A["Write Code"] --> B["Add Annotations"] B --> C["Maven Builds"] C --> D["Creates WAR file"] D --> E["Deploy to Server"] E --> F["App is Running!"]
Key Takeaways π―
-
Packaging = Putting your app in the right container (WAR, JAR, EAR)
-
Maven Coordinates = Address to find code (groupId:artifactId:version)
-
Deployment Descriptors = Instructions for the server (web.xml)
-
Annotations = Magic stickers that configure your code (@WebServlet, @Entity)
-
Annotations Basics = Start with @, can have parameters, replace most XML
You Did It! π
You now understand the foundation of every Jakarta EE project. These five concepts are like knowing how to read a map before going on an adventure.
Next time you see:
- A
.warfile β You know itβs a web app package! groupIdin XML β You know itβs an address!@WebServletβ You know itβs a magic sticker!
Youβre ready to build amazing enterprise applications! π
