- Prerequisites
- Project Structure
- Database Setup with Flask-SQLAlchemy
- Flask Services Setup
- Java JSP Frontend Setup
- Application Scenarios
- Testing the System
- API Documentation
- Troubleshooting
- Python 3.8+ - Download from python.org
- Java JDK 11+ - Download from Oracle or OpenJDK
- MySQL 8.0+ - Download from mysql.com
- Apache Tomcat 10.x - Download from tomcat.apache.org
- IDE (Optional but recommended):
- IntelliJ IDEA for Java development
- VS Code or PyCharm for Python
Create the following directory structure:
ecommerce-system/
โโโ ecommerce_system.sql (optional - for manual setup only)
โโโ order_service/ (Port 5001)
โ โโโ app.py
โ โโโ config.py
โ โโโ models.py
โ โโโ resources.py
โ โโโ requirements.txt
โโโ inventory_service/ (Port 5002)
โ โโโ app.py
โ โโโ config.py
โ โโโ models.py
โ โโโ resources.py
โ โโโ requirements.txt
โโโ pricing_service/ (Port 5003)
โ โโโ app.py
โ โโโ config.py
โ โโโ models.py
โ โโโ resources.py
โ โโโ requirements.txt
โโโ customer_service/ (Port 5004)
โ โโโ app.py
โ โโโ config.py
โ โโโ models.py
โ โโโ resources.py
โ โโโ requirements.txt
โโโ notification_service/ (Port 5005)
โ โโโ app.py
โ โโโ config.py
โ โโโ models.py
โ โโโ resources.py
โ โโโ requirements.txt
โโโ Ecommerce_jsp/
โโโ pom.xml
โโโ src/
โโโ main/
โโโ java/
โ โโโ servlets/
โ โโโ OrderServlet.java
โ โโโ ProfileServlet.java
โ โโโ OrderHistoryServlet.java
โ โโโ CheckoutServlet.java
โโโ webapp/
โโโ main_page.jsp
โโโ checkout.jsp
โโโ confirmation.jsp
โโโ profile.jsp
โโโ view_orders_history.jsp
# Login to MySQL
mysql -u root -p
# Create database only
CREATE DATABASE ecommerce_system;
# Create user (optional but recommended)
CREATE USER 'ecommerce_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON ecommerce_system.* TO 'ecommerce_user'@'localhost';
FLUSH PRIVILEGES;
exit;Note: We will NOT manually create tables. Flask-Migrate will handle all table creation automatically!
We use Flask-SQLAlchemy with Flask-Migrate to:
- โ Define models in Python (no manual SQL)
- โ Auto-generate database tables
- โ Track database changes with migrations
- โ Easy rollback/upgrade of schema
For each of the 5 services, create a requirements.txt file:
Flask==3.0.0
Flask-RESTful==0.3.10
Flask-SQLAlchemy==3.1.1
Flask-Migrate==4.0.5
mysqlclient==2.2.0
requests==2.31.0For each service (order, inventory, pricing, customer, notification):
# Navigate to service directory
cd order_service
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On Linux/Mac:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate config.py in each service:
class Config:
# Database configuration
SQLALCHEMY_DATABASE_URI = 'mysql://ecommerce_user:secure_password@localhost/ecommerce_system'
SQLALCHEMY_TRACK_MODIFICATIONS = False
# Service URLs for inter-service communication
ORDER_SERVICE_URL = 'http://localhost:5001'
INVENTORY_SERVICE_URL = 'http://localhost:5002'
PRICING_SERVICE_URL = 'http://localhost:5003'
CUSTOMER_SERVICE_URL = 'http://localhost:5004'
NOTIFICATION_SERVICE_URL = 'http://localhost:5005'Important: Update the database credentials:
- Replace
ecommerce_userwith your MySQL username (or useroot) - Replace
secure_passwordwith your MySQL password
For each service, run these commands:
# Make sure you're in the service directory with venv activated
cd order_service
source venv/bin/activate # or venv\Scripts\activate on Windows
# Initialize migrations (only first time)
flask --app app db init
# Create initial migration
flask --app app db migrate -m "Initial migration"
# Apply migrations to database
flask --app app db upgradeWhat happens:
flask db init- Createsmigrations/folderflask db migrate- Detects model changes and generates migration scriptflask db upgrade- Applies migrations and creates tables in MySQL
Repeat for all 5 services!
After migrations, you can add sample data via Python or SQL:
# Option 1: Python shell
flask --app app shell
>>> from models import Inventory, db
>>> product = Inventory(product_name='Laptop', quantity_available=50, unit_price=999.99)
>>> db.session.add(product)
>>> db.session.commit()
>>> exit()
# Option 2: MySQL (if you prefer)
mysql -u ecommerce_user -p ecommerce_system < sample_data.sqlOpen 5 separate terminal windows, one for each service:
Terminal 1 - Order Service (Port 5001):
cd order_service
source venv/bin/activate # or venv\Scripts\activate on Windows
python app.pyTerminal 2 - Inventory Service (Port 5002):
cd inventory_service
source venv/bin/activate
python app.pyTerminal 3 - Pricing Service (Port 5003):
cd pricing_service
source venv/bin/activate
python app.pyTerminal 4 - Customer Service (Port 5004):
cd customer_service
source venv/bin/activate
python app.pyTerminal 5 - Notification Service (Port 5005):
cd notification_service
source venv/bin/activate
python app.pyEach service should display:
* Running on http://0.0.0.0:500X
* Debug mode: on
Test each service:
# Inventory Service - Get all products
curl http://localhost:5002/api/inventory/products
# Customer Service - Get all customers
curl http://localhost:5004/api/customers/all
# Pricing Service - Calculate price
curl -X POST http://localhost:5003/api/pricing/calculate \
-H "Content-Type: application/json" \
-d '{"products":[{"product_id":1,"quantity":2}]}'-
Create New Project
- File โ New Project โ Java with Maven โ Web Application
- Project Name:
jsp-frontend - Server: Apache Tomcat 10.x
-
Add Dependencies to pom.xml
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
</dependencies>-
Create Servlets
- Create servlets for each scenario (see Application Scenarios section)
- Place in
src/main/java/servlets/
-
Create JSP Pages
- Create all JSP pages (index, checkout, confirmation, profile, view_orders_history)
- Place in
src/main/webapp/
-
Build and Deploy
- Build โ Build Project
- Run โ Run on Tomcat
- IntelliJ will deploy to Tomcat automatically
cd jsp-frontend
# Build the WAR file
mvn clean package
# Deploy to Tomcat
cp target/jsp-frontend.war /path/to/tomcat/webapps/
# Start Tomcat
cd /path/to/tomcat/bin
./startup.sh # Linux/Mac
startup.bat # WindowsFlow:
-
Main Page (index.jsp)
- Display all products where
quantity > 0 - Links to: Profile page, Orders history page
- Customer selects products and quantities
- Click "Make Order" button
- Display all products where
-
CheckoutServlet
- Validates: selected quantity โค available quantity
- Calls Inventory Service:
GET /api/inventory/check/{product_id} - Calls Pricing Service:
POST /api/pricing/calculate - Forwards to
checkout.jsp
-
Checkout Page (checkout.jsp)
- Shows products, quantities, and prices
- Customer can: Cancel (go to main) or Confirm
-
OrderServlet (when customer confirms)
- Reads: customer_id, products, quantities, total_amount
- Calls Order Service:
POST /api/orders/create - Calls Customer Service:
PUT /api/customers/{customer_id}/loyalty - Calls Notification Service:
POST /api/notifications/send - Forwards to
confirmation.jsp
-
Confirmation Page (confirmation.jsp)
- Shows order details and success message
Endpoints Used:
GET /api/inventory/products(Inventory Service)GET /api/inventory/check/{product_id}(Inventory Service)POST /api/pricing/calculate(Pricing Service)POST /api/orders/create(Order Service)PUT /api/customers/{customer_id}/loyalty(Customer Service)POST /api/notifications/send(Notification Service)
Flow:
-
Customer clicks "Profile" link on main page
-
ProfileServlet is called
- Reads customer_id from session
- Calls Customer Service:
GET /api/customers/{customer_id} - Gets customer details including loyalty points
- Forwards to
profile.jsp
-
Profile Page (profile.jsp)
- Displays customer information
- Shows loyalty points
Endpoints Used:
GET /api/customers/{customer_id}(Customer Service)
Flow:
-
Customer clicks "View Orders History" on main page
-
OrderHistoryServlet is called
- Reads customer_id from session
- Calls Customer Service:
GET /api/customers/{customer_id}/orders - For each order, calls Order Service:
GET /api/orders/{order_id} - Aggregates all order details
- Forwards to
view_orders_history.jsp
-
Order History Page (view_orders_history.jsp)
- Displays all previous orders
- Shows order details, products, amounts, dates
Endpoints Used:
GET /api/customers/{customer_id}/orders(Customer Service)GET /api/orders/{order_id}(Order Service)
Check that you have:
- โ 5 Flask services running (ports 5001-5005)
- โ MySQL database with tables created by migrations
- โ Sample data in database
- โ Tomcat server running (port 8080)
- โ JSP application deployed
USE ecommerce_system;
SHOW TABLES;
-- You should see tables:
-- orders, inventory, customers, pricing_rules, tax_rates, notification_log-- Sample products
INSERT INTO inventory (product_name, quantity_available, unit_price) VALUES
('Laptop', 50, 999.99),
('Mouse', 200, 29.99),
('Keyboard', 150, 79.99),
('Monitor', 75, 299.99),
('Headphones', 100, 149.99);
-- Sample customers
INSERT INTO customers (name, email, phone, loyalty_points) VALUES
('Ahmed Hassan', 'ahmed@example.com', '01012345678', 100),
('Sara Mohamed', 'sara@example.com', '01098765432', 250),
('Omar Ali', 'omar@example.com', '01055555555', 50);
-- Sample pricing rules
INSERT INTO pricing_rules (product_id, min_quantity, discount_percentage) VALUES
(1, 5, 10.00),
(2, 10, 15.00),
(3, 10, 12.00);
-- Sample tax rates
INSERT INTO tax_rates (region, tax_rate) VALUES
('Egypt', 14.00),
('Default', 10.00);Open your browser and navigate to:
http://localhost:8080/Ecommerce_jsp/main_page.jsp
- Browse products on main page
- Select products and quantities
- Click "Make Order"
- Verify prices on checkout page
- Click "Confirm"
- Check confirmation page shows order details
- Click "Profile" link
- Verify customer details displayed
- Check loyalty points shown
- Click "View Orders History" link
- Verify all previous orders displayed
- Check order details are complete
Check database changes:
-- Check orders created
SELECT * FROM orders ORDER BY created_at DESC LIMIT 5;
-- Check inventory updated
SELECT * FROM inventory;
-- Check loyalty points updated
SELECT name, loyalty_points FROM customers;
-- Check notifications logged
SELECT * FROM notification_log ORDER BY sent_at DESC LIMIT 5;Check API endpoints:
# Get all products (only with quantity > 0)
curl http://localhost:5002/api/inventory/products
# Get customer profile
curl http://localhost:5004/api/customers/1
# Get customer order history
curl http://localhost:5004/api/customers/1/orders
# Get specific order
curl http://localhost:5001/api/orders/1| Method | Endpoint | Description | Used In |
|---|---|---|---|
| GET | /api/inventory/products |
Get all products (quantity > 0) | Scenario 1 - Main Page |
| GET | /api/inventory/check/{product_id} |
Check stock availability | Scenario 1 - Checkout |
| PUT | /api/inventory/update |
Update inventory | Internal |
| Method | Endpoint | Description | Used In |
|---|---|---|---|
| POST | /api/pricing/calculate |
Calculate order total with discounts/tax | Scenario 1 - Checkout |
| Method | Endpoint | Description | Used In |
|---|---|---|---|
| POST | /api/orders/create |
Create new order | Scenario 1 - Order Confirmation |
| GET | /api/orders/{order_id} |
Get order details | Scenario 3 - Order History |
| GET | /api/orders?customer_id=X |
Get orders by customer | Internal |
| Method | Endpoint | Description | Used In |
|---|---|---|---|
| GET | /api/customers/all |
Get all customers | Internal |
| GET | /api/customers/{customer_id} |
Get customer profile | Scenario 2 - Profile |
| GET | /api/customers/{customer_id}/orders |
Get customer order history | Scenario 3 - Order History |
| PUT | /api/customers/{customer_id}/loyalty |
Update loyalty points | Scenario 1 - After Order |
| Method | Endpoint | Description | Used In |
|---|---|---|---|
| POST | /api/notifications/send |
Send order notification | Scenario 1 - After Order |
| GET | /api/notifications/history/{customer_id} |
Get notification history | Internal |
Error: No such command "db"
Solution:
# Make sure Flask-Migrate is installed
pip install Flask-Migrate
# Verify app.py has proper setup
# from flask_migrate import Migrate
# migrate = Migrate(app, db)Error: Can't locate revision identified by 'xxxxx'
Solution:
# Delete migrations folder and start over
rm -rf migrations/
flask --app app db init
flask --app app db migrate -m "Initial"
flask --app app db upgradeError: Access denied for user 'ecommerce_user'@'localhost'
Solution:
-- Check user exists
SELECT User, Host FROM mysql.user WHERE User='ecommerce_user';
-- Grant privileges
GRANT ALL PRIVILEGES ON ecommerce_system.* TO 'ecommerce_user'@'localhost';
FLUSH PRIVILEGES;Error: Failed building wheel for mysqlclient
Solution:
# On Ubuntu/Debian
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
# On MacOS
brew install mysql
# On Windows
# Download mysqlclient wheel from:
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
pip install mysqlclientโxxx.whlError: Address already in use: bind
Solution:
# Find and kill process
lsof -i :5001 # Linux/Mac
netstat -ano | findstr :5001 # Windows
kill -9 <PID> # Linux/Mac
taskkill /PID <PID> /F # WindowsError: Connection refused when calling other services
Solution:
- Verify all 5 services are running
- Check service URLs in
config.py - Test each service individually with curl
- Check firewall settings
# 1. Create database
mysql -u root -p
CREATE DATABASE ecommerce_system;
# 2. For each service:
cd service_name
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# 3. Initialize migrations (each service)
flask --app app db init
flask --app app db migrate -m "Initial"
flask --app app db upgrade
# 4. Start service
python app.py
# 5. Build and deploy JSP
cd Ecommerce_jsp
mvn clean package
# Deploy to Tomcat
# 6. Access application
http://localhost:8080/Ecommerce_jsp/main_page.jspIf you encounter issues:
- Check the troubleshooting section above
- Review service logs in terminal windows
- Check Flask migration status:
flask --app app db current - Verify database tables:
SHOW TABLES;in MySQL - Check Tomcat logs in
tomcat/logs/