Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Electricity Bill Servlet Application

This is a Java Servlet-based web application for managing electricity bills.

Project Structure

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)

Setup Instructions

1. Create the Database

Open phpMyAdmin → Go to SQL tab

Copy and paste the entire contents of DATABASE_SETUP.sql and execute it.

This creates:

  • electricity_billing_servlet database
  • users table (stores user accounts)
  • monthly_bills table (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)

2. Download Required JAR Files

Download and place these two files in WEB-INF/lib/:

  1. mysql-connector-j-8.4.0.jar

  2. jbcrypt-0.4.jar

3. Compile Java Files

  1. Open PowerShell

  2. Navigate to the src folder:

    cd d:\VIT\TY\WT\Lab Codes\ElectricityBillServlet\src
    
  3. 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

4. Deploy to Tomcat

Copy the entire ElectricityBillServlet folder to:

C:\xampp\tomcat\webapps\

5. Start Tomcat

Open XAMPP Control Panel → Click Start for Apache and MySQL

6. Access the Application

Open browser and navigate to:

http://localhost:8080/ElectricityBillServlet/login

Features

User Authentication

  • Register: Create new account with name, email, password
  • Login: Secure login using BCrypt password hashing
  • Logout: End session and redirect to login

Bill Management

  • 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

Database Schema

Users Table

id              INT (Primary Key)
name            VARCHAR(100)
email           VARCHAR(120) UNIQUE
password        VARCHAR(255) (BCrypt hashed)
created_at      TIMESTAMP

Monthly Bills Table

id              INT (Primary Key)
user_id         INT (Foreign Keyusers.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

Key Differences from PHP Version

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 ($2y$) BCrypt ($2a$ normalized)
Session Attributes user_email user_id, user_name
Bill URL /calculate.php /calculate

Troubleshooting

Compilation Errors

  • Ensure JAR files are in WEB-INF/lib/
  • Check Java version (JDK 11+)
  • Verify paths are correct (no spaces issues)

Database Connection Errors

  • Verify MySQL is running (XAMPP Control Panel)
  • Check database name: electricity_billing_servlet
  • Verify credentials in DBConnection.java: user=root, password=""

Page Not Found Errors

  • Verify web.xml servlet mappings
  • Check URL patterns in browser match web.xml definitions
  • Ensure .class files exist in WEB-INF/classes/

Login Issues

  • Check password hashing is working (BCrypt)
  • Verify user exists in database: SELECT * FROM users;
  • Check email is correct

Development Notes

  • 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)

About

Servelet Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages