Project Based Learning in Java
Unit 3 · Chapters 3.1 and 3.2
JavaServer Pages (JSP) is the original name of Jakarta Server Pages, a template technology that generates web content on the server. Application Programming Interface (API) defines the operations one software component can use to communicate with another. Java Persistence API (JPA) lets Java code save and load objects as database rows. The standard is now called Jakarta Persistence.
Servlet/JSP, Spring Core, Spring Boot, Hibernate/JPA, REST APIs and design patterns.
Course Outcome (CO) states what a learner should be able to do after the course; CO1 to CO5 label the five syllabus outcomes.
Enterprise Java · CO5
Chapter 3.1: From web requests to persistent applications
Inversion of Control (IoC) lets a framework create and manage application objects. In Spring, the container supplies these objects to the classes that need them.
Learn Servlet/JSP, Spring IoC and dependency injection, bean lifecycle, Boot configuration, Hibernate/JPA relationships, transactions and application error handling.
Java Database Connectivity (JDBC) is Java's standard interface for connecting to relational databases, issuing statements and reading results. Model-View-Controller (MVC) separates application data and rules, user-facing presentation, and request or input handling.
Prerequisites: classes and interfaces, exceptions, collections, JDBC and MVC from Units 1 and 2.
Start Chapter 3.1 →Study sequence
- Servlets and JSP
- Spring Core, Dependency Injection and Bean Lifecycle
- Spring Boot Setup and Application Configuration
- JPA Entities and Relationships
- Spring Data JPA and Transactions
- Exception Handling and Custom Error Responses
Representational State Transfer (REST) is an architectural style that organises a web service around resources, such as students or products, accessed through a consistent interface.
Continue to Chapter 3.2: REST APIs and Design Patterns. Complete Practical 3.2 and choose a project in Practical 3.3.
Create, Read, Update and Delete (CRUD) are the four basic operations used to manage stored records.
Chapter 3.2: REST APIs and Design Patterns
Study Chapter 3.2 in six lessons: resources, implementation, JSON, clients, object creation and behaviour patterns.
Worked examples
Web Application Archive (WAR) packages a Java web application for deployment to a compatible servlet container.
Download Servlet/JSP project (ZIP) · Java 17, Maven and Tomcat 10.1. Build a WAR and deploy it to practise request handling and JSP views.
H2 Database Engine (H2) is a Java relational database often used in memory for isolated examples; H2 is a product name, not an acronym.
Download student-management project (ZIP) · Java 17, Maven and Spring Boot 3.5.7. Includes H2, CRUD, relationship mappings, rollback tests and MVC error handling.
Extract each project and follow its README. The first Maven build needs internet access. All database data in the Boot example is temporary. Run the examples locally.
Chapter review
- Explain servlet lifecycle and request safety.
- Compare forwarding and redirection.
- Choose a dependency injection technique and justify it.
- Trace bean initialisation and destruction.
- Explain Boot auto-configuration and property overrides.
- Distinguish JPA, Hibernate and Spring Data JPA.
- Identify association ownership and fetching decisions.
- Explain dirty checking, rollback and transaction proxies.
- Design a clear custom error response.
Chapter 3.1 syllabus coverage
The syllabus topics for Chapter 3.1 are mapped below to their lessons and required explanations.
Expression Language (EL) is the expression syntax used by page technologies to read values and invoke supported operations. Data Transfer Object (DTO) carries selected data between application layers, such as the fields needed in a request or response. Hypertext Transfer Protocol (HTTP) defines how a client sends a request and a server returns a response on the web.
| Syllabus topic | Lesson | Coverage |
|---|---|---|
| Introduction to Servlet | Servlets and the Web Request Lifecycle | Definition, container, request–response model, mapping and lifecycle. |
| Introduction to JSP | JSP and MVC for Server-rendered Pages | JavaServer Pages/Jakarta naming, MVC, page lifecycle, EL and tags. |
| Introduction to Spring | Spring IoC and Dependency Injection | Framework purpose, dependencies, coupling and managed assembly. |
| IoC container | Spring IoC and Dependency Injection | Bean definitions, BeanFactory, ApplicationContext and container responsibilities. |
| Bean lifecycle | Spring Beans, Configuration and Lifecycle | Construction, injection, callbacks, post-processing, proxies and destruction. |
| @Component | Spring Beans, Configuration and Lifecycle | General stereotype and component scanning. |
| @Service | Spring Beans, Configuration and Lifecycle | Application/business-service stereotype and its limits. |
| @Repository | Spring Beans, Configuration and Lifecycle | Persistence stereotype, exception translation and Spring Data distinction. |
| @Autowired | Spring IoC and Dependency Injection | Injection points, type resolution and eligible candidates. |
| @Configuration | Spring Beans, Configuration and Lifecycle | Java configuration classes and bean-method behaviour. |
| @Bean | Spring Beans, Configuration and Lifecycle | Factory-method registration, a Clock example and callback configuration. |
| Constructor injection | Spring IoC and Dependency Injection | Required collaborators, explicit construction contract and final fields. |
| Setter injection | Spring IoC and Dependency Injection | Post-construction wiring and required/optional dependency distinction. |
| Field injection | Spring IoC and Dependency Injection | Annotated fields and construction/testing trade-offs. |
| Spring Boot auto-configuration | Spring Boot Setup and Application Configuration | Starters, conditional configuration, package layout and backing off. |
| application.properties | Spring Boot Setup and Application Configuration | Database/server settings, custom keys, profiles and common overrides. |
| Spring Data JPA with Hibernate | Spring Data JPA Repositories and CRUD | Repository interfaces, generated implementation, derived queries and CRUD. |
| @Entity and @Table | JPA Entities and Hibernate | Entity identity, table mapping, constructors and field/property access. |
| @Id and @GeneratedValue | JPA Entities and Hibernate | Primary keys, generated identifiers and generation strategies. |
| @OneToMany and @ManyToOne | JPA Relationships and Mapping Ownership | Foreign key, owning/inverse sides, mappedBy and in-memory consistency. |
| @ManyToMany | JPA Relationships and Mapping Ownership | Join-table ownership, shared records and an enrolment-entity alternative. |
| @Transactional | Transaction Management with @Transactional | Service boundaries, propagation, rollback rules, flush and proxy limitations. |
| @ExceptionHandler | Exception Handling and Custom Error Responses | Mapping a particular controller failure to a response. |
| @ControllerAdvice | Exception Handling and Custom Error Responses | Shared MVC handlers and the distinction from RestControllerAdvice. |
| Custom error responses | Exception Handling and Custom Error Responses | Error DTO, HTTP status, safe messages and tested response bodies. |
Chapter 3.2 covers REST API design, Jackson, REST clients and design patterns. The small HTTP controller in Chapter 3.1 supports the persistence and exception-handling examples.
Reference lecture mapping
The supplied lecture filenames use a different sequence from the six combined lessons. Use this map to find the corresponding material.
| Reference document and slide deck | Main content | E-content location |
|---|---|---|
| 3.1.1 | Servlet/JSP, web tiers and Spring overview | Lesson 1, Lesson 1, Lesson 2 |
| 3.1.2 | IoC, injection, lifecycle and scopes | Lesson 2, Lesson 2 |
| 3.1.3 | Annotation families | Lesson 2, with persistence and web details in later lessons |
| 3.1.4 | Boot, Hibernate architecture and entity mapping | Lesson 3, Lesson 4, Lesson 5 |
| 3.1.5 | JPA concepts, queries and association mappings | Lesson 4, Lesson 4, Lesson 5 |
| 3.1.6 | Propagation, isolation, validation and error handling | Lesson 5, Lesson 6 |
| 3.1.7 | REST, Jackson and API testing | REST and HTTP, Spring REST, JSON and Jackson |
| 3.1.8 | REST clients and Postman | REST clients |
| 3.1.9 | Creational design patterns | Singleton and Factory |
| 3.1.10 | Behavioural patterns and Spring events | Strategy and Observer |
The runnable examples use the chapter's Jakarta/Spring Boot baseline. Older API names in the reference notes are explained where relevant rather than mixed into the working projects.
HyperText Markup Language (HTML) structures a web page using elements such as headings, paragraphs, links and forms.
Foundations knowledge check
Check your understanding of the first two combined lessons. Select an answer for each question, then check your answers.
Answer key and explanations
- service(). service() dispatches to the HTTP-specific handler; init() and destroy() are lifecycle callbacks.
- WEB-INF/web.xml. web.xml is the deployment descriptor. Servlet annotations can also supply mappings.
- JSP view. The JSP is the View. The servlet coordinates the request, and application data/rules form the Model.
- Constructor injection. Constructor injection makes the requirement explicit and supports final fields and plain Java testing.
- PostConstruct. PostConstruct runs during managed bean initialisation; PreDestroy is for eligible cleanup.