
Servlet Starter Kit - Modern Java Web Starter Project
Servlet Starter Kit - Modern Java Web Starter Project
Introduction
Servlet Starter Kit is a complete starter toolkit for Java Web application development, designed specifically for educational purposes. The project combines the most modern Java EE technologies with a beautiful user interface, allowing students and new developers to learn and build web applications quickly.
Key Features
1. Clear MVC Architecture
The project is organized according to the standard MVC (Model-View-Controller) pattern, making the code easy to maintain and extend:
- Controller Layer: Supports both Web Controllers and AJAX Controllers.
BaseWebControllerandBaseAjaxControllerprovide shared utility methods.- Smart routing with path segment parsing.
- Flash message handling for user notifications.
- Service Layer: Separates business logic from the controller.
- Repository Layer: Repository Pattern with Generic Types supporting CRUD operations.
- Model Layer: Base Model with Lombok annotations.
2. Flexible Page Rendering System
The Page class offers a modern approach to rendering views:
1Page page = new Page()
2 .setTitle("Home Page")
3 .setPageName("home/index")
4 .setLayout("app")
5 .setScripts(new String[]{"user-management.js"})
6 .setStyleSheets(new String[]{"custom.css"});
7
8page.render(request, response);Advantages:
- Fluent API with method chaining.
- Automatic management of scripts and stylesheets.
- Support for layouts and partial views.
- Dynamic data passing via Map.
3. Powerful Database Integration
JDBI Integration
- Uses JDBI 3 - a lightweight and efficient ORM for Java.
- HikariCP connection pooling for performance optimization.
- Generic Repository Pattern to minimize boilerplate code.
1@ApplicationScoped
2public class UserRepository extends BaseRepository<User, Long> implements IUserRepository {
3 // Inherits all CRUD operations from BaseRepository
4}
5Auto Database Migration
- Automatically runs migrations when the application starts.
- Tracks executed migrations via the
schema_migrationstable. - Supports automatic database creation if it does not exist.
- Easily manage schema changes via SQL files.
1@WebListener
2public class DatabaseMigrationListener implements ServletContextListener {
3 // Auto-run migrations on startup
4}4. Modern UI with Tailwind CSS
- Responsive Design: Interface automatically adapts to all devices.
- Tailwind CSS 3: Utility-first CSS framework.
- Font Awesome Icons: Rich icon library.
- Component-based Layout: Header, Footer, and Navigation are separated.
UI Features:
- Dashboard displaying statistics.
- User Management Interface with DataTables.
- Form validation and error handling.
- Toast notifications.
- Modal dialogs.
5. Complete User Management
A fully functional user management module including:
- CRUD Operations: Create, read, update, delete users.
- AJAX Support: Operations without page reloads.
- Status Management: Activate/Deactivate users.
- Data Validation: Client-side and server-side validation.
- Flash Messages: Operational result notifications.
1@WebServlet("/ajax/users/*")
2public class AjaxUserController extends BaseAjaxController {
3 // Handles create, update, delete, activate, deactivate
4}6. Developer-Friendly Tools
Utilities Classes
PropertiesUtils: Reads configuration from properties files.WebAppPaths: Manages paths for static resources and views.BaseModelMapper: Automatically maps database rows to objects.
Filters
JsCharsetFilter: Ensures UTF-8 encoding for JavaScript files.
Dependency Injection
- CDI (Contexts and Dependency Injection) integration.
@Injectannotation for dependency injection.@ApplicationScopedbeans.
7. Simple Configuration
The project uses properties files for configuration:
1# Database Configuration
2db.driver=org.h2.Driver
3db.url=jdbc:h2:~/servlet-kit-db
4db.username=sa
5db.password=
6db.name=app_db
78. Maven Build System
- Maven wrapper included (no manual Maven installation required).
- Well-managed dependencies.
- Supports fast build and deployment.
1./mvnw clean packageProject Structure
servlet-starter-kit/
├── src/main/
│ ├── java/com/nphuonha/servletkit/
│ │ ├── core/ # Core utilities
│ │ │ ├── database/ # DB connection, migrations
│ │ │ ├── listeners/ # Servlet listeners
│ │ │ ├── paths/ # Path management
│ │ │ └── utils/ # Utility classes
│ │ ├── http/
│ │ │ ├── controllers/ # Web & AJAX controllers
│ │ │ ├── filters/ # Request filters
│ │ │ └── pages/ # Page rendering
│ │ ├── models/ # Domain models
│ │ ├── repository/ # Data access layer
│ │ └── services/ # Business logic
│ ├── resources/
│ │ ├── migrations/ # SQL migration files
│ │ └── application.properties
│ └── webapp/
│ ├── static/ # CSS, JS, images
│ └── views/ # JSP templates
│ ├── layouts/ # Layout templates
│ └── pages/ # Page views
├── pom.xml
├── tailwind.config.js
└── package.json
Use Cases
This project is suitable for:
- Learning Java Web: Understanding clearly how Servlets and JSP work.
- Student Projects: A complete starter kit to kickstart assignments.
- Rapid Prototyping: Building MVPs for web application ideas.
- MVC Practice: Learning and applying design patterns.
Technology Stack
Backend:
- Java 17+
- Jakarta Servlet 6.0
- Jakarta CDI 4.0
- JDBI 3 + HikariCP
- Lombok
Frontend:
- Tailwind CSS 3
- Font Awesome 6
- DataTables
- Vanilla JavaScript
Build Tools:
- Maven
- PostCSS + Autoprefixer
Important Note
Disclaimer: This project is designed for educational purposes. It should not be used directly in production without a thorough security review and optimization.
Key Highlights
- Clean Code: Code is clearly organized, readable, and easy to understand.
- Best Practices: Applies standard design patterns.
- Modern Stack: Uses the latest Java EE technologies.
- Easy to Extend: Flexible architecture, easy to add new features.
- Educational Focus: Highly suitable for teaching and learning.
What Will You Learn?
- How to build a web application with Java Servlet.
- Understanding the MVC pattern and how to apply it.
- Working with databases using JDBI.
- Dependency Injection with CDI.
- Modern frontend development with Tailwind CSS.
- AJAX and REST API basics.
- Database migration strategies.
Repository: nphuonha2101/servlet-starter-kit