부재: 스프링 부트 환경에서 서블릿을 등록하고, HTTP 요청과 응답 흐름을 직접 확인하자.


1. 서블릿 소개

2. 스프링 부트 서블릿 환경 구성

@ServletComponentScan : 서블릿을 자동으로 스캔하여 서블릿 컨테이너에 등록한다.

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

3. 서블릿 생성 및 등록

@WebServlet : 서블릿 이름과 URL 매핑 정보를 설정한다.

@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("HelloServlet.service");

        String username = request.getParameter("username");
        System.out.println("username = " + username);

        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("Hello " + username);
    }
}

4. HttpServletRequest