-
[스프링MVC - 1편] Hello 서블릿스프링&스프링부트 2024. 12. 25. 14:02
스프링부트 서블릿 환경 구성
@ServletComponentScan // 서블릿 자동 등록 @SpringBootApplication public class ServletApplication { public static void main(String[] args) { SpringApplication.run(ServletApplication.class, args); } }
@WebServlet(name="helloServlet", urlPatterns = "/hello") public class HelloServelt extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // super.service(req, resp); System.out.println("HelloServelt.service"); System.out.println("request = " + request); System.out.println("response = " + response); // HelloServelt.service // request = org.apache.catalina.connector.RequestFacade@5ab1f700 // response = org.apache.catalina.connector.ResponseFacade@3b8cd469 String username = request.getParameter("username"); System.out.println("username = " + username); response.setContentType("text/plain"); response.setCharacterEncoding("utf-8"); response.getWriter().write("hello " + username); } }
- @WebServlet 서블릿 애노테이션
- name : 서블릿 이름 (중복 불가)
- urlPatterns : URL 매핑
- http 요청을 통해 매핑된 URL이 호출되면 서블릿 컨테이너는 service 메서드 실행
HTTP 요청 메시지 로그로 확인하기
application.properties 추가 후 재실행한다
logging.level.org.apache.coyote.http11=trace
콘솔에서 확인 가능하다
서블릿 컨테이너 동작 방식
내장 톰캣 서버 생성한다
웹브라우저에서 HTTP 요청 메세지 만들어서 서버에 전달한다
서버는 HTTP 요청 메세지를 기반으로 request/response 객체 생성한다
객체 생성 후 싱글톤으로 존재하는 helloServlet을 호출한다
모든 로직이 끝난 후 WAS서버가 response 정보를 가지고 HTTP 응답 메세지를 생성 후 웹브라우저에 반환한다
webapp 에 index.html 웰컴페이지를 생성하고 http://localhost:8080/ 로 들어갔을 때 Whitelabel Error Page가 발생하는 현상이 있었다
빌드도 다시 해봤지만 문제를 해결할 수 없었다
https://www.inflearn.com/community/questions/479520/webapp-index-html-%EA%B4%80%EB%A0%A8
webapp index.html 관련 - 인프런 | 커뮤니티 질문&답변
누구나 함께하는 인프런 커뮤니티. 모르면 묻고, 해답을 찾아보세요.
www.inflearn.com
그러던 중 나와 동일한 현상이 발생하는 질문 글을 발견했다
처음 intelliJ에서 프로젝트를 open할 때 문제가 발생한 것이었다
intelliJ에서 프로젝트를 오픈할때
1)과 같이 해당 폴더만 선택하고 열기 버튼을 통해 오픈할 수 있고
2)와 같이 build.gralde 파일을 선택해 오픈할 수 있었다
나의 경우 1의 방법으로 프로젝트를 오픈했고 webapp의 index.html을 열 수 없었다
다시 2의 방법으로 프로젝트를 오픈하니 index.html이 정상적으로 열리는 것을 확인할 수 있었다
1)의 경우에는 빈 모듈(spring-mvc)을 통해 프로젝트를 여는 것이고
2)의 경우에는 빈 모듈 안에서 생성한 새로운 모듈(servlet)의 build.gradle을 통해 프로젝트를 여는 것이다
1)의 경우로 오픈한다면 모듈을 2개 불러와서 실행하는 것이니 인텔리제이가 인식할 수 없다....????
728x90'스프링&스프링부트' 카테고리의 다른 글
[스프링MVC - 1편] HTTP 요청 데이터 (2) 2025.01.02 [스프링MVC - 1편] HttpServletRequest (0) 2025.01.02 [intelliJ / springboot] 'Java/JavaVirtualMachines/openjdk-23.0.1/Contents/Home/bin/java' finished with non-zero exit value 1 (0) 2024.12.25 [스프링MVC - 1편] 자바 백엔드 웹 기술 역사 (0) 2024.12.22 [스프링MVC - 1편] HTML, HTTP API, CSR, SSR (1) 2024.12.22 - @WebServlet 서블릿 애노테이션