What is Dispatcher Servlet in Spring? (original) (raw)

DispatcherServlet in Spring is the central component of the Spring MVC framework that acts as the front controller. It receives all incoming HTTP requests, forwards them to the appropriate controllers, and manages the response generation process including handler mapping and view resolution.

spring_mvc_architecture

Dispatcher Servlet

Working

Steps for setting up DispatcherServlet in Spring MVC

In a Spring MVC application, the DispatcherServlet acts as the front controller, handling all incoming requests. Follow these steps to set it up:

Note: We'll be using Spring Tool Suite 4 (STS) or Eclipse IDE for this project. Refer to this article for installation instructions.

Step 1: Create a Dynamic Web Project in STS (Spring Tool Suite) or Eclipse

Step 2: Add Spring Framework JARs

Step 3: Configure Apache Tomcat

Step 4: Configure DispatcherServlet in web.xml

frontcontroller-dispatcher org.springframework.web.servlet.DispatcherServlet 1 frontcontroller-dispatcher /student.com/*

`

Step 5: Create Spring Configuration File

Step 6: Run Your Application

Steps for setting up DispatcherServlet in Spring Boot

In a Spring Boot application, the DispatcherServlet is automatically configured as part of the Spring Web dependency. Here's a simplified explanation of setting it up:

Step 1: Create a Spring Boot Project

1. Go to Spring Initializr
2. Project Configuration:

3. Click on Generate, which will download a .zip file of the Spring Boot project.
4. Unzip the file and open it in your IDE (e.g., Eclipse, or STS).

Step 2: Import the Project into Your IDE

**1. Import the Project:

**2. Wait for Maven to Download Dependencies:

Step 3: Create a Model Class

Java `

package com.example.demo.model;

public class Details { private int id; private String name;

// Constructor
public Details(int id, String name) {
    this.id = id;
    this.name = name;
}

// Getters and Setters
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

`

Step 4: Create a Controller Class

Java `

package com.example.demo.controller;

import com.example.demo.model.Details; import org.springframework.web.bind.annotation.*;

import java.util.ArrayList; import java.util.List;

@RestController @RequestMapping("/api") public class DetailsController {

private List<Details> detailsList = new ArrayList<>();

// GET endpoint to fetch all details
@GetMapping("/details")
public List<Details> getAllDetails() {
    return detailsList;
}

// POST endpoint to add new detail
@PostMapping("/details")
public String addDetails(@RequestBody Details details) {
    detailsList.add(details);
    return "Data Inserted Successfully";
}

// PUT endpoint to update a detail by ID
@PutMapping("/details/{id}")
public String updateDetails(@PathVariable int id, @RequestBody Details updatedDetails) {
    for (Details details : detailsList) {
        if (details.getId() == id) {
            details.setName(updatedDetails.getName());
            return "Data Updated Successfully";
        }
    }
    return "Detail not found!";
}

// DELETE endpoint to remove a detail by ID
@DeleteMapping("/details/{id}")
public String deleteDetails(@PathVariable int id) {
    detailsList.removeIf(details -> details.getId() == id);
    return "Data Deleted Successfully";
}

}

`

Step 5: Create the Spring Boot Main Application Class

package com.example.demo;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class DemoApplication {

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

}

`

Step 6: Configure DispatcherServlet (Optional in Spring Boot)

package com.example.demo.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.DispatcherServlet;

@Configuration public class WebConfig {

@Bean
public ServletRegistrationBean<DispatcherServlet> dispatcherServlet() {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    ServletRegistrationBean<DispatcherServlet> registrationBean = new ServletRegistrationBean<>(dispatcherServlet);
    registrationBean.addUrlMappings("/api/*");  // Customize URL mapping
    return registrationBean;
}

}

`

Step 7: Run the Spring Boot Application

http://localhost:8080.