Prof. (Dr. ) Harish Kumar MittalProfessor · Researcher · Educator

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

  1. Servlets and JSP
  2. Spring Core, Dependency Injection and Bean Lifecycle
  3. Spring Boot Setup and Application Configuration
  4. JPA Entities and Relationships
  5. Spring Data JPA and Transactions
  6. 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.

01Chapter 3.1 · Lesson 1Servlets and JSPFollow a web request from the servlet controller to the JSP view, and build a server-rendered page.02Chapter 3.1 · Lesson 2Spring Core, Dependency Injection and Bean LifecycleUnderstand how the Spring container creates, connects and manages application objects.03Chapter 3.1 · Lesson 3Spring Boot Setup and Application ConfigurationSet up a Spring Boot application and explain auto-configuration, properties and profiles.04Chapter 3.1 · Lesson 4JPA Entities and RelationshipsMap Java objects to database tables, then connect entities using clear ownership rules.05Chapter 3.1 · Lesson 5Spring Data JPA and TransactionsUse repositories for data access and service transactions to keep related changes consistent.06Chapter 3.1 · Lesson 6Exception Handling and Custom Error ResponsesTurn application failures into clear, consistent and safe HTTP responses.

Chapter 3.2: REST APIs and Design Patterns

Study Chapter 3.2 in six lessons: resources, implementation, JSON, clients, object creation and behaviour patterns.

Read Chapter 3.2 from the beginning · Practise with Lab 3.2

01Chapter 3.2 · Lesson 1REST Resources and HTTP MethodsDesign resource URLs and choose methods, status codes and request contracts.02Chapter 3.2 · Lesson 2Building REST APIs with Spring BootTranslate the resource contract into controller methods, request binding and response handling.03Chapter 3.2 · Lesson 3JSON, Jackson and ValidationConvert between Java objects and JSON, then validate the data at the API boundary.04Chapter 3.2 · Lesson 4REST Clients: RestTemplate and WebClientCall an API, handle failures and distinguish synchronous calls from reactive pipelines.05Chapter 3.2 · Lesson 5Singleton and Factory PatternsControl object creation and distinguish a simple factory from Factory Method.06Chapter 3.2 · Lesson 6Strategy and Observer PatternsSelect interchangeable algorithms and notify subscribers while respecting transaction boundaries.

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

  1. Explain servlet lifecycle and request safety.
  2. Compare forwarding and redirection.
  3. Choose a dependency injection technique and justify it.
  4. Trace bean initialisation and destruction.
  5. Explain Boot auto-configuration and property overrides.
  6. Distinguish JPA, Hibernate and Spring Data JPA.
  7. Identify association ownership and fetching decisions.
  8. Explain dirty checking, rollback and transaction proxies.
  9. 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.7REST, Jackson and API testingREST and HTTP, Spring REST, JSON and Jackson
3.1.8REST clients and PostmanREST clients
3.1.9Creational design patternsSingleton and Factory
3.1.10Behavioural patterns and Spring eventsStrategy 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.

1. Which HttpServlet method normally dispatches an HTTP request to doGet() or doPost()?
2. Which file is the traditional servlet deployment descriptor?
3. In the chapter MVC example, which component presents the HTML page?
4. Which injection style usually makes required collaborators clearest?
5. Which annotation defines an initialisation callback after bean dependency injection?
Answer key and explanations
  1. service(). service() dispatches to the HTTP-specific handler; init() and destroy() are lifecycle callbacks.
  2. WEB-INF/web.xml. web.xml is the deployment descriptor. Servlet annotations can also supply mappings.
  3. JSP view. The JSP is the View. The servlet coordinates the request, and application data/rules form the Model.
  4. Constructor injection. Constructor injection makes the requirement explicit and supports final fields and plain Java testing.
  5. PostConstruct. PostConstruct runs during managed bean initialisation; PreDestroy is for eligible cleanup.