Page |1
REFERENCE MATERIAL
[Link]. (Computer
Science)
Server
Programming
Notes
(MR23-
1BSCC18)
*******************
Page |2
SERVER PROGRAMMING (MR23-1BSCC18)
SYLLABUS
UNIT – I:
Spring Framework: Spring Framework Architecture, Modules, Spring
Dependencies, Spring Initializer, Spring vs Struts in Java, Spring MVC
UNIT– II:
Spring Boot: Micro services, Architecture, Auto configurations, Initialize, Install
Spring Tool Suite IDE, Creating an application, Dependency Management (Maven
and Gradle), Annotations, YAML Files, Actuators, Spring Boot Rest APIs creation.
UNIT – III:
Spring Boot: Micro services, Architecture, Auto configurations, Initialize, Install
Spring Tool Suite IDE, Creating an application, Dependency Management (Maven
and Gradle), Annotations, YAML Files, Actuators, Spring Boot Rest APIs creation.
UNIT – IV:
Spring boot JDBC: JDBC login application, Data JDBC, Data JPA, Spring Boot JPA.
Spring Boot: Thyme leaf Templates, Spring Boot Security.
UNIT – V:
SQLite Integration with Spring Boot: Introduction to SQLite – Features and
Advantages – Comparison with MySQL and MongoDB – SQLite Data Types –
Installation and Database Creation – Basic SQL Commands (CREATE, INSERT,
UPDATE, DELETE, SELECT) – Configuring SQLite in Spring Boot – Connecting with
Spring Data JPA – Performing CRUD Operations – Exception Handling – Creating
Executable JAR and Deployment.
SERVER PROGRAMMING LAB
1. Creating a Simple Spring Framework Application Develop a simple Java project
using the Spring Framework to display “Welcome to Spring Framework”.
2. Spring Dependency Injection Example Demonstrate Constructor and Setter
Injection in Spring using XML configuration.
3. Spring MVC Basic Application Create a basic Spring MVC application that
returns a welcome message using Controller and View (JSP).
4. Spring Boot Project Setup Initialize a new Spring Boot project using Spring
Initializr and run it successfully in STS IDE.
5. Spring Boot REST API – GET Request Create a REST API to display a list of
students using @RestController and test with Postman.
6. Spring Boot REST API – POST Request Create an endpoint to add a new student
record and return success message in JSON format.
7. JDBC Login Application Create a simple login validation system using Spring
Boot JDBC with a sample database table.
8. Thymeleaf Integration Build a simple web page using Thymeleaf Template to
display student details fetched from the database.
9. Spring Boot Security Basics Implement a simple username-password
authentication using Spring Security and in-memory users.
10. Exception Handling Example Demonstrate exception handling in Spring Boot
using @ControllerAdvice and @ExceptionHandler.
11. File Upload and Download in Spring Boot Develop a small application to
upload and download files using Spring Boot.
12. Spring Boot + SQLite CRUD Application Connect Spring Boot with SQLite
database, perform CRUD operations, and package the project as an executable
JAR.
~:0:~
Page |3
Questions and Answers
Unit-I
1. What is the Spring Framework, and what problems does it aim
to solve in Java enterprise applications?
Spring Framework is a lightweight, open-source Java framework
used for developing enterprise-level applications. It provides a complete
programming and configuration model that helps developers build
reliable, scalable, and high-performance applications.
The primary problems Spring aims to solve include tight coupling
between components, complex configuration, and difficulty in
testing in traditional Java EE applications. Spring addresses these issues
using
Inversion of Control (IoC) and Dependency Injection (DI),
which manage object creation and dependency relationships
automatically, resulting in loosely coupled and easily testable code.
Spring also simplifies database access, transaction
management, web application development, and security through
consistent APIs and reusable components. It reduces boilerplate code and
supports seamless integration with technologies such as JDBC,
Hibernate, and JPA.
Overall, the Spring Framework improves developer productivity and
application maintainability by providing a modular, flexible, and efficient
approach to Java enterprise application development, making it widely
adopted in the industry.
==
Spring Framework
Spring is a lightweight and open-source Java framework used to
develop enterprise applications. It provides tools and APIs that make
application development simpler, cleaner, and more manageable.
Problems in Traditional Java EE Applications
1. Tight Coupling
o Objects depend directly on other objects.
Page |4
o Any change in one component requires changes in others.
o Makes applications hard to maintain and extend.
2. Complex Configuration
o Large XML files and verbose setup.
o Difficult to understand and manage.
3. Difficulty in Testing
o Objects cannot be easily replaced with mock objects.
o Unit testing becomes complicated.
Inversion of Control (IoC)
Spring controls object creation instead of the programmer.
The framework creates and manages objects (beans).
Developers focus on business logic, not object management.
Dependency Injection (DI)
Dependencies are injected automatically by Spring.
Objects do not create their own dependencies.
Results in:
o Loose coupling
o Easy testing
o Better flexibility
Simplified Enterprise Features
Spring provides consistent APIs for:
Database access (JDBC, Hibernate, JPA)
Transaction management
Web application development
Security
This avoids repetitive and complex code.
Reduced Boilerplate Code
Eliminates repetitive code like connection handling, exception
management, etc.
Makes applications shorter, cleaner, and easier to read.
Benefits of Spring Framework
Improved developer productivity
Better application maintainability
Page |5
Modular and flexible design
Widely used in real-world industry applications
Spring Framework simplifies Java enterprise application development
by providing IoC and DI to reduce tight coupling, simplify configuration,
improve testability, and enhance productivity.
==
2. Explain the core architecture of the Spring Framework. What
are its main components?
The core architecture of the Spring Framework is based on the
principle of Inversion of Control (IoC) and is designed in a modular
way, allowing developers to use only the required components.
The main component of Spring architecture is the Spring
Container, which manages the lifecycle and configuration of application
objects called beans. The container uses Dependency Injection (DI) to
provide required dependencies to beans.
The core container consists of modules such as Core, Beans, Context,
and SpEL.
The Core and Beans modules provide IoC and DI functionality.
The Context module provides application context, event handling,
and resource loading.
The SpEL (Spring Expression Language) module supports
querying and manipulating object graphs.
Apart from the core container, Spring provides other important
modules like AOP (Aspect-Oriented Programming) for cross-cutting
concerns, Data Access/Integration for database interaction, Web and
MVC for web applications, and Test module for unit and integration
testing.
Thus, Spring’s architecture is layered, modular, and flexible,
making it suitable for enterprise Java applications.
3. What are the different modules of the Spring Framework?
Briefly describe the purpose of each.
Page |6
The Spring Framework is divided into several modules, each
designed to provide specific functionality. This modular structure allows
developers to use only the required parts.
1. Core Container Module: Includes Core, Beans, Context, and
SpEL modules. It provides Inversion of Control (IoC) and
Dependency Injection (DI), bean management, and application
context features.
2. AOP (Aspect-Oriented Programming) Module: Supports
separation of cross-cutting concerns such as logging, security,
and transaction management from business logic.
3. Data Access / Integration Module: Provides support for JDBC,
ORM (Hibernate, JPA), Transactions, JMS, and simplifies
database interaction and transaction handling.
4. Web Module: Supports web application development with features
like servlets, web context, and multipart file upload.
5. Spring MVC Module: Implements the Model–View–Controller
design pattern for building flexible and loosely coupled web
applications.
6. Test Module: Provides support for unit testing and integration
testing using frameworks like JUnit and TestNG.
These modules together make Spring a powerful, flexible, and
comprehensive framework for enterprise Java application
development.
4. What is Dependency Injection in Spring, and how does it help
achieve loose coupling?
Dependency Injection (DI) in Spring is a design pattern in which
the Spring container automatically provides the required
dependencies (objects) to a class, rather than the class creating them
itself. This is a core part of Inversion of Control (IoC).
There are two main types of DI in Spring:
1. Constructor Injection – Dependencies are provided through a
class constructor.
Page |7
2. Setter Injection – Dependencies are provided through setter
methods.
How DI achieves loose coupling:
The dependent class does not need to know how to create or
manage its dependencies; it only specifies what it needs.
This reduces the dependency between classes, making the system
more modular, flexible, and easier to test.
Changes in one class (like implementation changes) do not affect
other classes that depend on it, improving maintainability.
Example:
Instead of a Car class creating its own Engine, Spring injects an Engine
object into Car. This allows different types of engines to be used without
modifying the Car class.
In short, DI promotes loose coupling, reusability, and
testability in Spring applications.
5. What are the different types of dependencies injection
supported by Spring?
Types of Dependency Injection Supported by Spring
Dependency Injection (DI) in Spring is a core concept of
Inversion of Control (IoC), where the Spring container provides
required objects (dependencies) to a class instead of the class creating
them.
Spring supports three main types of Dependency Injection:
1. Constructor Injection
Dependencies are provided through a class constructor.
Recommended for mandatory dependencies.
Ensures the object is fully initialized at creation time.
Supports immutability and easier unit testing.
Example:
public class Service
{
Page |8
private Repository repo;
public Service(Repository repo)
{
[Link] = repo;
}
}
Advantages:
Prevents NullPointerException
Best for required dependencies
Encourages clean design
2. Setter Injection
Dependencies are provided using setter methods.
Suitable for optional dependencies.
Allows changing dependencies after object creation.
Example:
public class Service
{
private Repository repo;
public void setRepo(Repository repo)
{
[Link] = repo;
}
}
Advantages:
Flexible configuration
Useful when dependency is optional
Disadvantage:
Object may be in an incomplete state
3. Field Injection
Dependencies are injected directly into fields using annotations.
No constructor or setter required.
Page |9
Commonly used with @Autowired.
Example:
public class Service
{
@Autowired
private Repository repo;
}
Advantages:
Less boilerplate code
Easy to use
Disadvantages:
Not recommended for testing
Breaks encapsulation
Hard to mock
Comparison Summary
Type Injection Use Case
Method
Constructor Constructor Mandatory
dependencies
Setter Setter method Optional dependencies
Field Direct field Quick development
Spring supports Constructor, Setter, and Field Dependency
Injection. Among them, Constructor Injection is the most
recommended due to better design, testability, and reliability.
6. What is Spring Initializr, and how does it simplify Spring
application development?
Spring Initializr is a web-based tool provided by the Spring team
to quickly create a Spring Boot project with the required configuration
and dependencies.
Spring Initializr:
It generates a ready-to-use Spring Boot project.
Available as:
o Web tool: [Link]
P a g e | 10
o Integrated into IDEs like Eclipse, IntelliJ IDEA, and Spring
Tool Suite (STS).
Automatically sets up:
o Project structure
o Build configuration (Maven/Gradle)
o Dependency management
Spring Initializr Simplifies Development
1. Automatic Project Setup
Creates standard Spring Boot folder structure.
Generates essential files like:
o [Link] or [Link]
o Main application class
o Configuration files
2. Easy Dependency Selection
Developers can select required dependencies such as:
o Spring Web
o Spring Data JPA
o Spring Security
Automatically resolves compatible versions, reducing conflicts.
3. Faster Development
Eliminates manual configuration.
Developers can start coding immediately.
Reduces setup time from hours to minutes.
4. Build Tool Configuration
Supports Maven and Gradle.
Pre-configures plugins and dependencies.
Ensures best practices by default.
5. IDE Integration
Directly accessible from popular IDEs.
Simplifies project import and management.
Improves developer productivity.
Advantages of Spring Initializr
Saves time and effort
P a g e | 11
Reduces configuration errors
Beginner-friendly
Ensures standard project structure
Spring Initializr simplifies Spring application development by
automating project creation, dependency management, and
configuration, allowing developers to focus on business logic instead
of setup.
7. What are the advantages of using Spring Framework over
traditional Java EE development?
The Spring Framework is a lightweight, modular framework that
simplifies enterprise Java application development compared to
traditional Java EE, which relies heavily on application servers and
complex configurations.
1. Lightweight and Modular
Spring is lightweight and does not require a full application server.
Developers can use only required modules (Core, MVC, Security,
Data).
Java EE applications are heavier and tightly coupled to servers.
2. Dependency Injection (IoC) Support
Spring provides powerful Dependency Injection, improving loose
coupling.
Java EE traditionally relies on container-managed components.
Results in better maintainability and testability.
3. Easier Configuration
Spring supports Java-based, XML, and annotation-based
configuration.
Reduces boilerplate code.
Java EE requires extensive XML configuration (older versions).
4. Better Testability
Spring applications are easy to test using JUnit and Mockito.
Supports unit and integration testing without a container.
P a g e | 12
Java EE often needs a running application server for testing.
5. Comprehensive Ecosystem
Spring provides ready-made solutions:
o Spring Boot
o Spring Data
o Spring Security
Java EE requires third-party tools for similar features.
6. Faster Development with Spring Boot
Auto-configuration reduces setup time.
Embedded servers (Tomcat, Jetty).
Java EE needs manual server configuration.
7. Better Exception Handling
Spring provides consistent and clear exception hierarchy.
Simplifies error handling compared to Java EE.
8. Community and Industry Adoption
Large developer community and strong documentation.
Widely adopted in industry and cloud-based applications.
Comparison Summary
Aspect Spring Traditional Java
Framework EE
Configuration Flexible & simple Complex
Server Optional Mandatory
Dependency
Testing Easy Difficult
Modularity High Limited
Spring Framework offers flexibility, simplicity, faster
development, and better testability compared to traditional Java EE,
making it the preferred choice for modern enterprise applications.
8. Compare Spring Framework and Struts. Highlight key
differences in architecture, flexibility, and performance.
Spring Framework and Struts are popular Java frameworks used for
enterprise and web application development. However, they differ
significantly in architecture, flexibility, and performance.
P a g e | 13
1. Architecture
Spring Framework
Based on Inversion of Control (IoC) and Dependency Injection
(DI).
Uses layered architecture (Controller, Service, DAO).
Supports Spring MVC which follows the Front Controller pattern.
Promotes loose coupling.
Struts
Based on MVC architecture.
Uses Action classes to handle requests.
Tightly coupled with web layer.
Limited support for layered enterprise applications.
2. Flexibility
Spring Framework
Highly flexible and modular.
Supports multiple technologies (Hibernate, JPA, JDBC).
Easy integration with other frameworks.
Configuration using annotations, XML, or Java config.
Struts
Less flexible.
Mainly focused on web applications.
Limited integration capabilities.
Heavily dependent on XML configuration.
3. Performance
Spring Framework
Lightweight and efficient.
Better performance due to optimized dependency handling.
Supports caching and transaction management.
Embedded servers improve execution speed.
Struts
Slower compared to Spring.
Uses centralized controllers and heavy configurations.
P a g e | 14
Higher memory usage.
4. Development and Testing
Spring Framework
Easy unit and integration testing.
POJO-based development.
Faster development using Spring Boot.
Struts
Difficult testing due to framework dependency.
Requires container for testing.
Slower development cycle.
5. Community and Support
Spring Framework
Actively maintained and widely used.
Large ecosystem and strong industry support.
Struts
Declining usage.
Limited updates and community support.
Comparison Table
Feature Spring Struts
Framework
Architecture IoC + MVC MVC only
Flexibility High Low
Performance High Moderate
Configuratio Annotations + XML-based
n Java
Testing Easy Difficult
Spring Framework is more flexible, scalable, and high-
performing than Struts. While Struts is suitable for simple MVC-based
applications, Spring is the preferred choice for modern enterprise
and cloud-based systems.
P a g e | 15
9. What is Spring MVC, and how does it implement the Model–
View–Controller design pattern?
Spring MVC is a web framework within the Spring Framework
used to develop web applications and RESTful services. It follows the
Model–View–Controller (MVC) design pattern, which separates
application logic, user interface, and request handling.
What is Spring MVC?
Part of the Spring Framework.
Used for building web-based applications.
Provides clean separation of concerns.
Uses Front Controller architecture.
MVC Components in Spring MVC
1. Model
Represents business data and logic.
Contains objects, services, and database interactions.
Independent of the user interface.
Example:
public class Student
{
private int id;
private String name;
}
2. View
Responsible for displaying data to the user.
Uses technologies like:
o JSP
o Thymeleaf
o HTML
Does not contain business logic.
3. Controller
Handles user requests.
Processes input and interacts with the Model.
Selects appropriate View for response.
P a g e | 16
Example:
@Controller
public class StudentController
{
@GetMapping("/students")
public String getStudents(Model model)
{
[Link]("list", students);
return "studentView";
}
}
How Spring MVC Implements MVC Pattern
Front Controller – DispatcherServlet
Central component of Spring MVC.
Receives all HTTP requests.
Delegates requests to appropriate controllers.
Request Flow in Spring MVC
1. Client sends request.
2. DispatcherServlet receives request.
3. Controller processes request.
4. Model is updated.
5. View renders response.
6. Response sent to client.
Advantages of Spring MVC
Loose coupling
Easy testing
Clean architecture
Supports REST APIs
Spring MVC effectively implements the MVC design pattern by
separating Model, View, and Controller, resulting in scalable,
maintainable, and testable web applications.
10. Explain the request flow in a Spring MVC application from
client request to response generation.
P a g e | 17
In a Spring MVC application, the request flow explains how a
client request is processed and a response is generated using the
Model–View–Controller (MVC) architecture.
Step-by-Step Request Flow
1. Client Request
The client (browser or REST client) sends an HTTP request.
Example:
[Link]
2. DispatcherServlet (Front Controller)
The request is first received by the DispatcherServlet.
It acts as the central controller.
Manages the entire request-response lifecycle.
3. Handler Mapping
DispatcherServlet consults HandlerMapping.
Identifies the appropriate Controller based on URL and HTTP
method.
4. Controller Execution
The matched Controller method is invoked.
Business logic is executed.
Controller interacts with Service and DAO layers.
5. Model Creation
Controller adds data to the Model object.
Model contains data to be displayed in the View.
6. View Resolver
DispatcherServlet uses ViewResolver.
Maps logical view name to an actual view (JSP/Thymeleaf).
7. View Rendering
View receives the Model data.
Generates the final HTML/JSON response.
8. Response to Client
DispatcherServlet sends the generated response back to the client.
Request Flow Diagram (Textual)
Client → DispatcherServlet → HandlerMapping
P a g e | 18
→ Controller → Model
→ ViewResolver → View
→ Response → Client
Key Advantages of This Flow
Clear separation of concerns
Centralized request handling
Easy maintenance and testing
Supports RESTful services
Spring MVC request flow ensures efficient request processing using
DispatcherServlet, Controller, Model, and View, resulting in
scalable and maintainable web applications.
……………………………………………………………………………………………………………