프로젝트 생성
패키지 만들때 하나만 주의하자. 보통은 Jar를 쓰는데, 이번엔 War를 써야 한다.
JSP를 사용하려면 War를 써야 한다.
hello서블릿
스프링부트는 톰캣서버를 내장하고 있다.. 바로 서블릿 코드를 실행해보자.
스프링부트는 다음의 애너테이션을 제공한다. 애플리케이션 메인 파일에 애너테이션을 붙여주자…
@ServletComponentScan //서블릿코드들을 자동으로 스캔해 컨테이너에 등록해준다.
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
서블릿 코드의 가장 기본적인 틀을 만들어보자.
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class helloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
서블릿 객체를 컨테이너에서 실행하면 그 내부의 서비스 메서드(비즈니스 로직)가 실행되는 것이 기본적인 구조이다.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("helloServlet.service");
System.out.println("request = " + request);
System.out.println("request = " + request);
}