This is a Java Servlet-based web application for managing electricity bills.
ElectricityBillServlet
├── style.css (existing)
├── index.html (existing)
├── DATABASE_SETUP.sql (SQL database setup)
├── src/
│ ├── DBConnection.java (Database connection manager)
│ ├── HtmlUtil.java (HTML generation utilities)
│ ├── HomeServlet.java (Home/redirect page)
│ ├── LoginServlet.java (Login handler)
│ ├── RegisterServlet.java (Registration handler)
│ ├── DashboardServlet.java (Dashboard display)
│ ├── CalculateBillServlet.java (Bill calculation)
│ ├── HistoryServlet.java (Bill history display)
│ └── LogoutServlet.java (Logout handler)
│
└── WEB-INF/
├── web.xml (Servlet configuration)
├── classes/ (Compiled .class files)
└── lib/
├── mysql-connector-j-8.4.0.jar (MySQL driver)
├── jbcrypt-0.4.jar (Password hashing)
└── README.md (Library instructions)
Open phpMyAdmin → Go to SQL tab
Copy and paste the entire contents of DATABASE_SETUP.sql and execute it.
This creates:
electricity_billing_servletdatabaseuserstable (stores user accounts)monthly_billstable (stores bill records with one bill per user per month)
Key features:
- UNIQUE constraint on
(user_id, bill_month)ensures one bill per user per month - Average is calculated dynamically using SQL
AVG()function - Foreign key ensures data integrity (deletes bills when user is deleted)
Download and place these two files in WEB-INF/lib/:
-
mysql-connector-j-8.4.0.jar
- Download from: https://dev.mysql.com/downloads/connector/j/
- Select version 8.4.0 from dropdown
-
jbcrypt-0.4.jar
- Download from: http://www.mindrot.org/projects/jBCrypt/
- Direct link: https://www.mindrot.org/files/jBCrypt/jbcrypt-0.4.jar
-
Open PowerShell
-
Navigate to the
srcfolder:cd d:\VIT\TY\WT\Lab Codes\ElectricityBillServlet\src -
Compile all Java files:
javac -cp "C:\xampp\tomcat\lib\servlet-api.jar;..\WEB-INF\lib\mysql-connector-j-8.4.0.jar;..\WEB-INF\lib\jbcrypt-0.4.jar" -d ..\WEB-INF\classes *.java
If compilation succeeds, you'll see these .class files in WEB-INF/classes/:
- DBConnection.class
- HtmlUtil.class
- HomeServlet.class
- LoginServlet.class
- RegisterServlet.class
- DashboardServlet.class
- CalculateBillServlet.class
- HistoryServlet.class
- LogoutServlet.class
Copy the entire ElectricityBillServlet folder to:
C:\xampp\tomcat\webapps\
Open XAMPP Control Panel → Click Start for Apache and MySQL
Open browser and navigate to:
http://localhost:8080/ElectricityBillServlet/login
- Register: Create new account with name, email, password
- Login: Secure login using BCrypt password hashing
- Logout: End session and redirect to login
-
Calculate Bill: Enter month and units consumed, system calculates bill
- Rate: ₹5.50 per unit
- Enforces: One bill per user per month
-
View History: See all saved monthly bills
- Displays month, units consumed, and bill amount
- Shows average bill calculated from all saved records
-
Dashboard: Overview of billing statistics
- Total months with saved bills
- Total amount paid
- Average monthly bill
id INT (Primary Key)
name VARCHAR(100)
email VARCHAR(120) UNIQUE
password VARCHAR(255) (BCrypt hashed)
created_at TIMESTAMPid INT (Primary Key)
user_id INT (Foreign Key → users.id)
bill_month DATE
units INT
bill_amount DECIMAL(10,2)
created_at TIMESTAMP
UNIQUE(user_id, bill_month) -- One bill per user per month| Feature | PHP Version | Servlet Version |
|---|---|---|
| Framework | PHP 7+ | Java Servlet (Jakarta) |
| Database Table | bills |
monthly_bills |
| Date Column |
bill_date DATETIME |
bill_month DATE |
| Connection Types | Domestic/Commercial | Fixed rate (₹5.50) |
| Password Hashing | BCrypt ( |
BCrypt ( |
| Session Attributes | user_email |
user_id, user_name
|
| Bill URL | /calculate.php |
/calculate |
- Ensure JAR files are in
WEB-INF/lib/ - Check Java version (JDK 11+)
- Verify paths are correct (no spaces issues)
- Verify MySQL is running (XAMPP Control Panel)
- Check database name:
electricity_billing_servlet - Verify credentials in
DBConnection.java: user=root, password=""
- Verify web.xml servlet mappings
- Check URL patterns in browser match web.xml definitions
- Ensure .class files exist in WEB-INF/classes/
- Check password hashing is working (BCrypt)
- Verify user exists in database:
SELECT * FROM users; - Check email is correct
- All HTML generation is centralized in
HtmlUtil.java - All database operations use try-with-resources (auto-close)
- Input validation on both client (HTML) and server (Java) side
- Password hashing uses BCrypt with salt generation
- Session timeout follows Tomcat defaults (30 minutes)