# JAVA18

jwebserver 구동

공식사이트에서 캡처 이미지를 이용해보겠다.

<figure><img src="https://3202568828-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgVNjdXkr3ciD8p5jXPCI%2Fuploads%2F24GkO4nkR7ozekDuGMBg%2Fjava-jwebserver.png?alt=media&#x26;token=0687781a-a29d-4bc7-8ef6-e46e20e3f1d7" alt=""><figcaption></figcaption></figure>

짧은 영어능력으로 일단 이해한 내용을 요약하면!

* Jetty、Nginx 등 기존 웹서버를 대체하기위함이 아닌 순수 테스트,학습목적으로 만들어 졌다.
* 인증, 시큐리티, 액세스컨트롤, 암호화 등 기능은 지원하지 않는다.
* HTTP/1.1 만 지원하고 HTTPS는 지원 안한다!
* GET、HEAD 요청만 지원한다.
* 명령을 통한 Java 클래스 구동이 가능하다.

아주 간단한 index.html  파일하나 만들어보자!

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
    <h1>안녕하세요 신비한 비 티스토리 블로그 입니다!</h1>
    </div>
    <div><h3>티스토리블로그:</h3></div>
    <url>https://rainsister.tistory.com/</url>
    <div><h3>깃북블로그:</h3></div>
    <url>https://app.gitbook.com/o/oa8n0se1zcxXR1QCxPlj/home</url>
    
</body>
</html>
```

내가 설치한 java 버전 그리고 home 경로를 파악해보자. 맥 기준 터미널에서

```bash
/usr/libexec/java_home -V
Matching Java Virtual Machines (7):
    18.0.2.1 (x86_64) "Eclipse Adoptium" - "OpenJDK 18.0.2.1" /Users/blake/Library/Java/JavaVirtualMachines/temurin-18.0.2.1/Contents/Home
    17.0.5 (arm64) "Eclipse Adoptium" - "OpenJDK 17.0.5" /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
    16.0.2 (x86_64) "Eclipse Temurin" - "Eclipse Temurin 16" /Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home
    11.0.17 (arm64) "Eclipse Adoptium" - "OpenJDK 11.0.17" /Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home
    11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_345 (x86_64) "Eclipse Temurin" - "Eclipse Temurin 8" /Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home
    1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
```

/Users/blake/Library/Java/JavaVirtualMachines/temurin-18.0.2.1/Contents/Home 로 확인 인된다.

jwebserver 를 시작하기 위해 bin 폴더까지 이동한다.

```
./jwebserver
Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
Serving /Users/blake/Library/Java/JavaVirtualMachines/temurin-18.0.2.1/Contents/Home/bin and subdirectories on 127.0.0.1 port 8000
URL http://127.0.0.1:8000/
127.0.0.1 - - [05/1월/2023:16:16:31 +0900] "GET /index.html HTTP/1.1" 200 -
127.0.0.1 - - [05/1월/2023:16:16:59 +0900] "GET /index.html HTTP/1.1" 200 -
```

추가 설명!

```bash
-h or -? or --help
 Prints the help message and exits.

-b addr or --bind-address addr
 Specifies the address to bind to.  Default: 127.0.0.1 or ::1 (loopback).  For
 all interfaces use -b 0.0.0.0 or -b ::.

-d dir or --directory dir
 Specifies the directory to serve.  Default: current directory.

-o level or --output level
 Specifies the output format.  none | info | verbose.  Default: info.

-p port or --port port
 Specifies the port to listen on.  Default: 8000.

-version or --version
 Prints the version information and exits.
```

* `-b`：바인딩할 ip
* `-p`：포트
* `-d`：제공할 목록&#x20;
* `-o`：console 출력등급

```bash
jwebserver -p 9000 -d / -b 127.0.0.1 -o info
```

<figure><img src="https://3202568828-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgVNjdXkr3ciD8p5jXPCI%2Fuploads%2FKrq2zR8mWxxsLoGdygLQ%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202023-01-05%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.03.56.png?alt=media&#x26;token=c64d3cf7-3b3e-46d5-a6f3-24a07dbb9ee7" alt=""><figcaption></figcaption></figure>

\
루트 / 디렉토리의 내용을 확인 할수 있다.

## jwebserver 구동(with java code)

테스트용 메인함수 하나 만든다.

```java
public static void main(String[] args) {
    System.out.println("구동완료!!!");
    
    var addr = new InetSocketAddress(9898);
    var server = SimpleFileServer.createFileServer(
        addr, Path.of("/"), SimpleFileServer.OutputLevel.INFO);
    server.start();
}
```

<figure><img src="https://3202568828-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgVNjdXkr3ciD8p5jXPCI%2Fuploads%2FJsglN4iB6CkAKPpdguKu%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202023-01-05%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.15.11.png?alt=media&#x26;token=fabe5225-07bc-4c34-922b-0f68e9da3a83" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3202568828-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgVNjdXkr3ciD8p5jXPCI%2Fuploads%2Fm9vD0nIaMowVJialBdTA%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202023-01-05%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.15.21.png?alt=media&#x26;token=6918bfba-3dc6-4bf6-94e5-e07ec609c9d5" alt=""><figcaption></figcaption></figure>

확인을 위해 9898 포트 새로 띄여보면 정상적으로 시작된걸 알수 있다.

끝!
