A responsive web app that calculates an electricity bill using slab-wise (tiered) pricing, built with Java Servlets (controller/logic), JSP (views), and styled with Bootstrap 5 + jQuery for client-side validation.
| Slab | Units | Rate / Unit |
|---|---|---|
| 1 | First 50 units | Rs. 3.50 |
| 2 | Next 100 units (51–150) | Rs. 4.00 |
| 3 | Next 100 units (151–250) | Rs. 5.20 |
| 4 | Above 250 units | Rs. 6.50 |
Billing is tiered, meaning each slab is charged only for the units that fall inside it — e.g. for 300 units:
50 units × Rs. 3.50 = Rs. 175.00
100 units × Rs. 4.00 = Rs. 400.00
100 units × Rs. 5.20 = Rs. 520.00
50 units × Rs. 6.50 = Rs. 325.00
--------------------------------
Total = Rs. 1420.00
ElectricityBillCalculator/
├── pom.xml
├── src/main/java/com/billcalc/
│ └── BillCalculatorServlet.java # Handles form submission + slab-wise calculation
└── src/main/webapp/
├── index.jsp # Input form (Bootstrap + client-side validation)
├── result.jsp # Bill breakdown / summary page
├── css/style.css # Custom styling
├── js/validate.js # jQuery form validation
└── WEB-INF/web.xml # Deployment descriptor
- Backend: Java Servlet (
javax.servlet, compatible with Tomcat 8.x / 9.x) - Views: JSP
- Frontend: Bootstrap 5 (via CDN), Bootstrap Icons, jQuery 3.7 (via CDN)
- Build: Maven (packaged as a
.war)
Note on Tomcat version: This project uses the classic
javax.servletnamespace, which runs on Tomcat 8/9. If you're using Tomcat 10+ (which moved tojakarta.servlet), update the imports inBillCalculatorServlet.javafromjavax.servlet.*tojakarta.servlet.*and swap the Maven dependency coordinates inpom.xmlaccordingly.
- Make sure you have JDK 8+, Maven, and Apache Tomcat 8/9 installed.
- From the project root, build the WAR:
mvn clean package
- Copy the generated
target/ElectricityBillCalculator.warinto Tomcat'swebapps/folder. - Start Tomcat, then open:
http://localhost:8080/ElectricityBillCalculator/
- Import the project as an existing Maven project.
- Add a Tomcat 8/9 server runtime in your IDE.
- Right-click the project → Run on Server.
- The app will open at
index.jsp.
index.jsp— the form collects the consumer's name and units consumed. jQuery (js/validate.js) validates the inputs in the browser before submitting (non-empty name, valid non-negative number of units).BillCalculatorServlet(/calculateBill) — receives the POST request, re-validates on the server side (never trust client input), applies the tiered slab calculation, and forwards the results toresult.jspusingrequest.setAttribute(...)+RequestDispatcher.result.jsp— renders a slab-wise breakdown table and the final payable amount using Bootstrap styling.
- All UI is responsive (mobile, tablet, desktop) via Bootstrap's grid and utility classes.
- Server-side validation is the source of truth; jQuery validation is purely for a smoother UX.
- Meter rent / service charge fields are included in the calculation and
currently default to 0 — easy to change in
BillCalculatorServlet.javaif your assignment requires fixed charges.