부재: 스프링 부트 환경에서 서블릿을 등록하고, HTTP 요청과 응답 흐름을 직접 확인하자.
@ServletComponentScan : 서블릿을 자동으로 스캔하여 서블릿 컨테이너에 등록한다.
@ServletComponentScan
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
@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);
}
}
http://localhost:8080/hello?username=worldHello world클라이언트의 요청 정보를 담고 있는 객체입니다.
쿼리 파라미터 확인:요청 URL: http://localhost:8080/hello?username=kim콘솔 출력: username = kim
String username = request.getParameter("username");
System.out.println("username = " + username)