[스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술] Hello Servlet

2024. 8. 26. 15:59·Back-End/Spring

[작성일: 2024. 04. 03]

 

Hello Servlet

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

스프링부트는 서블릿을 직접 등록해서 사용할 수 있도록 @servletComponentScan을 지원한다. 

package hello.servlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan // 서블릿 자동 등록 애노테이션
@SpringBootApplication
public class ServletApplication {
	public static void main(String[] args) {
		SpringApplication.run(ServletApplication.class, args);
	}
}

 

 

 

서블릿 등록하기

package hello.servlet.basic;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@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");
        System.out.println("request = " + request);
        System.out.println("response = " + response);

        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이 호출되면 서블릿 컨테이너는 다음 메서드를 실행하게 된다.

protected void service(HttpServletRequest request, HttpServletResponse response)

 

 

  • 웹 브라우저 실행
    • http://localhost:8080/hello?username=world
    • 결과 : hello world

콘솔 실행결과

 

 

 

HTTP 요청 메시지 로그 확인하기

application.properties에 다음과 같은 설정을 추가하면 서버가 받은 HTTP 요청 메시지를 출력하는 것을 확인할 수 있다.

하지만 운영 서버에서 이렇게 모든 요청 정보를 다 남기게 되면 성능 저하가 발생할 수 있으므로 개발 단계에서만 적용하도록 한다.

logging.level.org.apache.coyote.http11=debug

 

 

 

 

 

서블릿 컨테이너 동작 방식

내장 톰캣 서버 생성

출처: 김영한님
출처: 김영한님

 

 

 

웹 애플리케이션 서버의 요청 응답 구조

출처: 김영한님

 

 

 

 

 

 

 

 

 

 


🐣 출처: 인프런 김영한님 강의

 

이 글은 인프런의 김영한님 스프링 강의를 보고 작성한 글입니다.

강의를 들으면서 정리한 글이므로 틀린 내용이나 오타가 있을 수 있습니다.

 

저작자표시 비영리 변경금지 (새창열림)
'Back-End/Spring' 카테고리의 다른 글
  • [스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술] HTTP 요청 데이터
  • [스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술] HttpServletRequest
  • [스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술] 웹 애플리케이션 이해
  • [스프링 핵심 원리 - 기본편] 빈 스코프
뚜비
뚜비
1년차 백엔드&iOS 개발자의 감자 탈출 블로그 🥔🥔
  • 뚜비
    뚜비의 개발로그
    뚜비
  • 전체
    오늘
    어제
  • 글쓰기     관리
    • Devlog
      • Back-End
        • Java
        • Spring
        • JPA
        • HTTP
        • Security
        • Back-End
        • Front-End
      • 알고리즘
      • iOS
        • Swift
      • Database
      • Tips
        • Git & GitHub
        • A to Z
      • 프로젝트
      • 생각정리
  • 태그

    spring
    MVC
    성능최적화
    Security
    김영한
    Swift
    데이터베이스
    스프링
    최주호
    JPA
    변수
    Spring Security
    객체
    다형성
    html
    백준
    jsp
    DB
    생성자
    자바
    프로그래머스
    의존성주입
    Java
    자바스크립트
    HTTP
    javascript
    게시판만들기
    Database
    알고리즘
    sql
  • hELLO· Designed By정상우.v4.10.0
뚜비
[스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술] Hello Servlet
상단으로

티스토리툴바