> For the complete documentation index, see [llms.txt](https://dodeon.gitbook.io/study/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dodeon.gitbook.io/study/keesunbaik-concepts-of-spring-boot/03-principle/embeded-server/container.md).

# 컨테이너와 서버 포트

## 다른 서블릿 컨테이너로 변경하기

```xml
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
      <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
  </dependency>

....
```

* 기존 톰캣을 빼고 jetty로 변경한다.

## 웹 서버 사용하지 않기

```properties
spring.main.web-application-type=none
```

* 모든 웹서버 설정을 무시하고 없앤다.

```
2022-08-20 07:24:42.956  INFO 32931 --- [           main] me.whiteship.Application                 : Starting Application using Java 11.0.11 on DodeonM1.local with PID 32931 (/Users/Dodeon/study/keesunbaik-concepts-of-spring-boot/target/classes started by Dodeon in /Users/Dodeon/study/keesunbaik-concepts-of-spring-boot)
2022-08-20 07:24:42.962  INFO 32931 --- [           main] me.whiteship.Application                 : No active profile set, falling back to 1 default profile: "default"
2022-08-20 07:24:43.750  INFO 32931 --- [           main] me.whiteship.Application                 : Started Application in 1.359 seconds (JVM running for 2.105)
holoman = Holoman{name='dodeon', howLong=33}
```

* 웹 서버 실행 없이 끝난다.

## 포트 변경하기

```properties
server.port=7070
```

```
INFO 33498 --- [           main] o.e.jetty.server.AbstractConnector       : Started ServerConnector@3402b4c9{HTTP/1.1, (http/1.1)}{0.0.0.0:7070}
```

## 랜덤 포트 사용하기

```properties
server.port=0
```

```
o.e.jetty.server.AbstractConnector       : Started ServerConnector@1816e24a{HTTP/1.1, (http/1.1)}{0.0.0.0:51102}
```

## 설정한 포트 사용하기

{% tabs %}
{% tab title=".java" %}

```java
// 웹 서버가 초기화 되면 이 이벤트 리스너가 호출된다.
@Component
public class PortListener implements ApplicationListener<ServletWebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(ServletWebServerInitializedEvent event) {
        ServletWebServerApplicationContext applicationContext = event.getApplicationContext();
        int port = applicationContext.getWebServer().getPort();

        System.out.println("port = " + port);
    }
}
```

{% endtab %}
{% endtabs %}

```
port = 52099
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dodeon.gitbook.io/study/keesunbaik-concepts-of-spring-boot/03-principle/embeded-server/container.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
