Spring Boot 에서 Servlet 사용하기

By | 2020년 3월 22일
Table of Contents

Spring Boot 에서 Servlet 사용하기

Spring Boot 에서 Servlet 사용은 보통 권장하지 않는 방법이지만… 사용해야할 필요성이 생기면 아래의 방법으로 서블릿을 사용할 수 있습니다.

@Configuration
public class ServletRegistrationConfig {

    @Bean
    public ServletRegistrationBean getServletRegistrationBean()
    {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HelloServlet());
        registrationBean.addUrlMappings("/hello");

        return registrationBean;
    }
}
public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
    {
        res.getWriter().print("hello");
    }
}

http://localhost:8080/hello 에 접속하면 서블릿이 실행된 것을 볼 수 있습니다.

답글 남기기