A small layered J2EE Book Management application built with JSP, Servlets, plain Java business logic, and an in-memory repository.
Request flow:
Browser
|
v
JSP / HTML
|
v
Servlet
|
v
BookService
|
v
BookRepository
|
v
In-memory List<Book>
The application keeps the responsibilities separated:
- Client layer: JSP/HTML presentation
- Web layer: Servlets and application lifecycle listener
- Business layer: BookService validates input and enforces the unique-ISBN rule
- Data layer: BookRepository owns the in-memory collection
The shared BookService instance is created when the web application starts through ApplicationContextListener and stored in ServletContext. Servlets retrieve it from the application context instead of managing its lifecycle themselves.
- Add a book with title, author, and ISBN
- Required-field validation
- Duplicate ISBN validation
- View all books
- Request-scoped validation errors
- Preserve submitted form values after validation failures
- Basic consistent JSP styling
- WAR deployment to Apache Tomcat
- JDK 8 or later
- Apache Tomcat 9
- javax.servlet-api.jar
Create the classes directory:
mkdir WebContent\WEB-INF\classesCompile:
javac -cp "path\to\javax.servlet-api.jar" -d WebContent\WEB-INF\classes src\com\example\model\Book.java src\com\example\data\BookRepository.java src\com\example\service\BookService.java src\com\example\web\ApplicationContextListener.java src\com\example\servlet\AddBookServlet.java src\com\example\servlet\ViewBooksServlet.javaPackage:
jar -cvf BookShelf.war -C WebContent .Deploy BookShelf.war to Tomcat webapps and open:
http://localhost:8080/BookShelf/
Books are stored only in memory and are lost when the application restarts.