1. New Project
- Jakarta EE 사용
- 이름, 위치, Template, Application Server, Build System 설정
- Template : Web application
- Application server : Tomcat 11.0.5 ( 자신이 설치한 Tomcat 버전 )

Server가 없을 때
- New 클릭
- Tomcat Server 클릭

- 폴더 모양 아이콘 클릭
- C:\Program Files\Apache Software Foundation\Tomcat 11.0 선택
- OK를 눌러 지정

- 폴더 구조 필히 확인
- 생성 시 필수 파일 다운로드 필요, 다운로드 완료 전까지는 서버를 작동시키지 말 것

2. Context Path
- Context Path : 파일의 위치를 파악할 수 있는 경로, 어플리케이션을 구분하기 위함
- 여러 개의 프로젝트 중 실행할 파일을 고를 수 있게 하는 경로
- CP를 생략(/)하게 되면, IP주소:포트번호만 입력해도 앱을 실행시킬 수 있음
- 네이버, 다음 등 포털사이트 등에서 도메인만 입력해도 바로 html을 실행하는 것과 같음

Context Path 설정 방법
- IntelliJ 상단의 Tomcat 11.0.5 클릭 → Edit Configurations 클릭
- Deployment 클릭
- Application context를 ‘/’로 변경

- 이후 WAP를 실행하면 뒤의 URI가 사라진 것을 확인 가능

3. Home Repository of WAP
- webapp, resources 두 곳에 각각 hello.css, style.css 파일 작성

- resources의 style.css에 링크 → 반영 안됨

- 파일 수정 후 즉시 반영이 안됨
- 서버 재 시작 필요 ( Redeploy )

- Redeploy 이후 반영된 것을 확인 가능

4. 보안 폴더
- WEB-INF 내부에 a.txt 파일 생성 후 홈페이지에서 실행을 해도 접근할 수 없음
- WEB_INF는 보안 폴더 : 외부에서 접근할 수 없음
- 내부에서는 접근 가능




package org.example.demo;
import java.io.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
// Client와 1:1 소통이 가능한 클래스
// Allocation : JVM이 보는 힌트 (Reflection) ->
@WebServlet("/hello.do")
public class HelloServlet extends HttpServlet {
@Override
// req : 요청 버퍼에 담긴 모든 텍스트를 객체화한 것
// resp : 응답 버퍼에 접근할 수 있는 객체 ( BufferedWriter 내장 )
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String html = """
<html>
<head>
<title>Servlet</title>
</head>
<body>
""";
for(int i=0;i<10;i++){
html = html + """
<h1>$i</h1>
""".replace("$i", i+"");
}
html = html+"""
</body>
</html>
""";
PrintWriter out = resp.getWriter();
out.println(html);
}
}

Share article