[JAVA] WAS

최재원's avatar
Mar 13, 2025
[JAVA] WAS

WAS란?

웹 어플리케이션 서버
응답 시 html 파일만 주는 웹 서버와 달리
응답 시 프로그램 실행 후 결과를 돌려주는 웹 어플리케이션 서버
notion image
notion image

URL & URI 설명

📌 URI (Uniform Resource Identifier, 통합 자원 식별자)

  • 자원을 식별하기 위한 문자열
  • URL과 URN을 포함하는 개념
  • URI는 자원의 위치를 나타낼 수도 있고, 단순히 식별만 할 수도 있음

URI 예시

  1. https://example.com/index.html(URL & URI 둘 다)
  1. mailto:user@example.com(이메일 주소, URI지만 URL 아님)
  1. ISBN:978-3-16-148410-0(URN, URI지만 URL 아님)

📌 URL (Uniform Resource Locator, 통합 자원 위치자)

  • URI의 하위 개념으로 자원의 "위치(주소)"를 나타내는 식별자
  • 즉, 웹사이트, 파일, API 엔드포인트 등의 접근 경로를 포함
  • 프로토콜(https, ftp 등) + 경로 + 도메인(IP 주소) 등으로 구성

URL 예시

  1. https://example.com/index.html(웹 페이지 주소)
  1. ftp://ftp.example.com/file.txt(FTP 파일 접근)
  1. https://api.example.com/v1/users(API 엔드포인트)

🔍 URL과 URI의 포함 관계

📌 "모든 URL은 URI지만, 모든 URI가 URL은 아니다!"
📌 URL은 "어디에 있는지(위치)"를 포함하지만, URI는 단순 식별도 가능
URL ⊂ URI (URL은 URI의 하위 개념!)

🛠 URL의 구조

🔹 https://example.com:8080/path/to/resource?query=123#section1
  • https:// → 프로토콜 (HTTP, HTTPS, FTP 등)
  • example.com → 도메인 (또는 IP 주소)
  • :8080 → 포트 번호 (기본값이 아닐 경우 표시)
  • /path/to/resource → 경로
  • ?query=123 → 쿼리 스트링 (추가 정보)
  • #section1 → 프래그먼트 (페이지 내 특정 위치)

📌 정리

개념
의미
예시
URI
자원을 식별하는 모든 문자열
https://example.com, mailto:user@example.com
URL
자원의 위치를 포함한 식별자
https://example.com/index.html
즉, URL은 URI의 한 종류로, "어디에 있는지"를 포함하는 개념! 🚀

WAS를 할 수 있는 서블릿 설치

  • 자바로 소켓통신(반이중)에 http프로톨콜까지 들어있는 프로젝트
  • 자바로 동적 page를 만드는 기술 = 서블릿 servlet
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
webapp폴더가 ws의 공유 폴더다

톰켓의 context path 제거

notion image
context path란?
웹 애플리케이션이 배포될 때 사용하는 기본 URL 경로
  • 하나의 서버(Tomcat) 안에 여러 개의 웹 애플리케이션을 배포할 수 있도록 함.
  • 클라이언트가 접근할 때 어떤 애플리케이션을 호출해야 하는지 구분하는 역할.
📌 예제
http://localhost:8080/myapp/
➡ 여기서 **/myapp**이 바로 Context Path 야.
➡ 만약 Context Path"/"(루트)라면 URL은 http://localhost:8080/이 됨.
notion image
notion image
notion image
notion image
http://localhost:8080/Gradle___org_example___deom_1_0_SNAPSHOT_war/ → http://localhost:8080/ 로 변경

톰켓 서버의 파일 경로 찾기

1. 폴더 경로

notion image

2. <http://ip주소:포트번호> 요청

notion image
  • 맨 뒤 ‘/’ 는 생략 가능
  • ip:port 다음에 아무것도 없으면 webapp폴더에 index.html파일을 찾는다

3. <http://ip주소:포트번호/파일명> 요청

notion image
  • 파일 이름으로 찾으면 webapp폴더로 간다
  • webapp폴더 안에 있는 hello.css파일을 찾아준다
notion image
notion image
notion image
  • WEB-INF폴더는 외부 요청을 거부한다.(보안폴더라서)
    • 저 폴더 안에 img, video등 파일을 넣어 보호한다.
  • index.html에서 WEB-INF안에 있는 파일을 요청하면 요청이 거부된다
    • index.html에서 요청하는 건 외부 브라우저가 요청하는 것이기 때문

 

WAS 실습

아파치 톰켓
아파치 : ws
톰켓 : was

경로

notion image

WAS 작동 방식

notion image

WAS 코드

package org.example.demo; 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; import java.io.PrintWriter; // 외부 클라이언트랑 1:1로 연결해주는 클래스 // @ = jvm이 보는 주석(힌트) @WebServlet("/h.do") // http://localhost:8080/h.do 요청하면 실행됨 public class HelloServlet extends HttpServlet { // req (요청 버퍼에 담긴 모든 text를 객체화 시킨 것) // resp (응답 버퍼에 접근할 수 있는 객체) @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String html = """ <html> <head> <title>Hello 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); } }
  • @ = jvm이 보는 힌트(주석)
  • @WebServlet("/h.do") 주소로 요청이 오면 HelloServlet 클래스가 실행됨
  • req = 요청 버퍼에 담긴 모든 text를 객체화 시킨 것
  • resp = 응답 버퍼에 접근할 수 있는 객체

결과

notion image

 
Share article

jjack1