
1. Jakarta EE로 프로젝트 생성

2. Tomcat 위치 설정
- 환경 변수로 설정해도 이해할 수 있는 프로토콜이 없어서 그냥 직접 설정해야 함


Tomcat을 사용할 수 있는 권한 허용하기

3. 사용할 라이브러리 설정하기

4. Context path 설정하기
- Context path(컨텍스트 경로)는 웹 애플리케이션을 서버에서 실행할 때 해당 애플리케이션이 서버에 배치될 때의 경로



연습 문제 1
package controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
public class HelloController {
public HelloController(){
System.out.println("HelloController 컴포넌트 스캔됨");
}
@GetMapping("home")
public void home() { //HelloControlle가 new 되서 찾을 수 있음, 다른 패키지는 뜨지 않음
System.out.println("home 호출됨");
}
@GetMapping("/hello")
public String hello(){
String name = "홍길동";
return "<h1>hello "+name+"</h1>";
}
@GetMapping("/random")
public String random(){
Random r = new Random();
int num = r.nextInt(5)+1; // 0부터 시작하니까 +1을 해줘야 함
return "<h1>random "+num+"</h1>";
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
연습 문제 2
package com.example.demo2;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
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("/*")
public class FrontController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service call");
}
}


연습문제 3
package com.example.demo2;
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("/*")
public class FrontController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service call");
System.out.println(req.getRequestURI());
System.out.println(req.getContextPath());
}
}

Share article