Integrating Servlets with Spring Boot: A Comprehensive Guide

Integrating Servlets with Spring Boot: A Comprehensive Guide

Introduction

In this article, we will delve into the process of integrating servlets with Spring Boot, a popular Java-based web framework. We will explore two methods of integration: the first method involves adding the @WebServlet annotation to the servlet, while the second method requires registering the servlet in the startup class.

Constructing the Project

To begin, we need to create a foundation for our Spring Boot project. This involves adding the necessary dependencies to the pom.xml file.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Integrated Way: Adding the @WebServlet Annotation

The first method of integrating a servlet with Spring Boot involves adding the @WebServlet annotation to the servlet.

  1. Create a Servlet: Create a servlet under the src directory and add the relevant get methods. In this example, we will create a servlet called FirstServlet.
/**
 * @Program: springboot-01-servlet
 * @Description: Spring Boot first way to integrate the servlet
 * @Author: Bobo duck
 * @Create: 2019-05-11 14:53
 */
@WebServlet(name = "FirstServlet", urlPatterns = "/first")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("- doGet method performed ----");
        PrintWriter out = resp.getWriter();
        out.write("success");
        out.flush();
        out.close();
    }
}
  1. Start Class Configuration: Configure the start class to load custom annotations by adding the @ServletComponentScan annotation.
/**
 * @Program: springboot-01-servlet
 * @Description: Spring Boot first way to integrate the servlet
 * @Author: Bobo duck
 * @Create: 2019-05-11 14:53
 */
@SpringBootApplication
@ServletComponentScan
public class Springboot01ServletApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01ServletApplication.class, args);
    }
}
  1. Start the Test Program: Start the program and access the servlet by navigating to http://localhost:8080/first. The integration is successful if you see the output “success”.

Second Way: Registering the Servlet in the Startup Class

The second method of integrating a servlet with Spring Boot involves registering the servlet in the startup class.

  1. Create a Servlet: Create a servlet under the src directory without adding the @WebServlet annotation. In this example, we will create a servlet called SecondServlet.
/**
 * @Program: springboot-01-servlet
 * @Description: Spring Boot second way to integrate the servlet
 * @Author: Bobo duck
 * @Create: 2019-05-11 15:00
 */
public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("- doGet method performed ----");
        PrintWriter out = resp.getWriter();
        out.write("success second");
        out.flush();
        out.close();
    }
}
  1. Modify the Startup Class: Create a new startup class and register the servlet by adding a ServletRegistrationBean object.
/**
 * @Program: springboot-01-servlet
 * @Description: Start class Spring Boot second way across the Servlet
 * @Author: Bobo duck
 * @Create: 2019-05-11 15:04
 */
@SpringBootApplication
public class App {

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

    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {
        // direct access ServletRegistrationBean object and associated custom servlet
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        // set corresponding servlet UrlMapping
        bean.addUrlMappings("/second");
        return bean;
    }
}
  1. Start the Test Program: Start the program and access the servlet by navigating to http://localhost:8080/second. The integration is successful if you see the output “success second”.

By following these steps, you can integrate servlets with Spring Boot using both the @WebServlet annotation and the ServletRegistrationBean object.