Introduction to Spring boot | Its Advantage over Spring MVC and Servlets

1๏ธโƒฃ Servlet and Servlet Container

๐Ÿ”น What is a Servlet?

  • A Servlet is a Java class that handles HTTP requests and generates HTTP responses.
  • It acts as a bridge between client and server.
  • Used mainly for building web applications.

Key Responsibilities:

  • Accept client request
  • Process business logic
  • Generate response (HTML / JSON / XML)

๐Ÿ”น What is a Servlet Container?

  • A Servlet Container manages the lifecycle of servlets.
  • Examples:
    • Tomcat
    • Jetty
    • WebLogic

Responsibilities:

  • Create servlet instances
  • Manage lifecycle (init, service, destroy)
  • Handle threading
  • Map URLs to servlets using web.xml or annotations

๐Ÿ”น Requestโ€“Response Flow

  1. Client sends HTTP request
  2. Request reaches Servlet Container (Tomcat)
  3. Container checks web.xml
  4. Identifies mapped servlet
  5. Invokes servlet
  6. Servlet processes and returns response


๐Ÿ”น Servlet Example

public class DemoServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) {
        String path = request.getPathInfo();
        if ("/firstendpoint".equals(path)) {
            // logic
        }
    }
}

๐Ÿ”น web.xml Mapping

<servlet>
    <servlet-name>DemoServlet</servlet-name>
    <servlet-class>DemoServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/demo/*</url-pattern>
</servlet-mapping>

โœ… Quick Summary

  • Servlet handles request/response
  • Servlet Container manages lifecycle
  • web.xml maps URLs to servlets
  • Tight coupling and manual configuration

โ“ Interview / Exam Questions

  1. What is a Servlet Container?
  2. Explain servlet lifecycle.
  3. Role of web.xml?
  4. Difference between doGet() and doPost()?

2๏ธโƒฃ Problems with Traditional Servlets

๐Ÿ”ธ Major Challenges

  • โŒ Too much XML configuration
  • โŒ Tight coupling
  • โŒ Difficult unit testing
  • โŒ Hard REST API management
  • โŒ No built-in integration support


โœ… Quick Summary

  • Servlets are low-level
  • Not scalable for large apps
  • Spring solves these issues

โ“ Interview / Exam Questions

  1. Why are Servlets not preferred today?
  2. What makes Servlets tightly coupled?
  3. Why is testing difficult in Servlets?

3๏ธโƒฃ Spring Framework Introduction

๐Ÿ”น What is Spring Framework?

  • A lightweight Java framework
  • Solves problems of Servlets
  • Based on Inversion of Control (IoC)

๐Ÿ”น Core Features

  • Dependency Injection
  • Loose coupling
  • Annotation-based configuration
  • Spring MVC
  • Easy integration


โœ… Quick Summary

  • Spring simplifies Java enterprise apps
  • Promotes clean architecture
  • Enables easy testing

โ“ Interview / Exam Questions

  1. What is Spring Framework?
  2. Advantages of Spring?
  3. Why Spring is lightweight?

4๏ธโƒฃ Dependency Injection (IoC)

๐Ÿ”น Without Dependency Injection

public class Payment {
    User sender = new User(); // tight coupling
}

Problems:

  • โŒ Tight coupling
  • โŒ Cannot mock
  • โŒ Hard to extend

๐Ÿ”น With Dependency Injection

@Component
public class Payment {

    @Autowired
    User sender;
}

๐Ÿ”น Key Annotations

  • @Component โ†’ Bean creation
  • @Autowired โ†’ Inject dependency


๐Ÿ”น Internal Working

  • Spring scans classes
  • Creates beans
  • Resolves dependencies
  • Injects at runtime

๐Ÿ”น Best Practices

  • Prefer constructor injection
  • Avoid field injection in production
  • Use interfaces

โœ… Quick Summary

  • DI removes tight coupling
  • Improves testability
  • Managed by Spring IoC container

โ“ Interview / Exam Questions

  1. What is Dependency Injection?
  2. Difference between IoC and DI?
  3. Types of DI in Spring?

5๏ธโƒฃ Spring MVC & DispatcherServlet

๐Ÿ”น What is DispatcherServlet?

  • Front Controller in Spring MVC
  • Handles all incoming requests

๐Ÿ”น Request Flow

  1. Request hits DispatcherServlet
  2. HandlerMapping identifies controller
  3. Controller method invoked
  4. Response returned


๐Ÿ”น Controller Example

@Controller
@RequestMapping("/payment")
public class PaymentController {

    @GetMapping("/details")
    public String getDetails() {
        return "details";
    }
}

โœ… Quick Summary

  • DispatcherServlet is central controller
  • Clean separation of concerns
  • Supports REST APIs

โ“ Interview / Exam Questions

  1. What is DispatcherServlet?
  2. Role of HandlerMapping?
  3. Difference between Controller & RestController?

6๏ธโƒฃ Spring Boot Introduction

๐Ÿ”น What is Spring Boot?

  • Built on top of Spring Framework
  • Used to create production-ready apps quickly

๐Ÿ”น Problems Solved by Spring Boot

  • Dependency management
  • Configuration complexity
  • Server setup

๐Ÿ”น Key Features

  1. Auto Configuration
  2. Embedded Server
  3. Starter Dependencies
  4. Convention over Configuration


๐Ÿ”น Main Class

@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

๐Ÿ”น Embedded Server

  • No WAR file
  • No external Tomcat
  • Just run main() method

๐Ÿ”น REST API Example

@RestController
@RequestMapping("/myapi")
public class MyController {

    @GetMapping("/firstapi")
    public String getData() {
        return "Hello from concept and coding";
    }
}

 


โœ… Quick Summary

  • Spring Boot accelerates development
  • Reduces configuration
  • Embedded server support

โ“ Interview / Exam Questions

  1. Difference between Spring & Spring Boot?
  2. What is Auto Configuration?
  3. What is Embedded Server?

๐Ÿ“Œ FINAL CONCLUSION

  • Servlets are foundation but limited
  • Spring introduces IoC and MVC
  • Spring Boot makes Spring production-ready
  • Ideal for microservices & REST APIs

 

Leave a Reply