-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현하기 - 1단계] 레오(차영호) 미션 제출합니다. (#387)
* 패키지 위치 변경 및 코드 정리 * 서블릿 학습 테스트 코드 개선 * test: 학습 테스트 * feat: mvc 구현하기 * feat: RequestMethod from 정팩메 작성 --------- Co-authored-by: kang-hyungu <[email protected]>
- Loading branch information
1 parent
b16924a
commit 3a3e4b0
Showing
50 changed files
with
472 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,3 +171,5 @@ Temporary Items | |
|
||
tomcat.* | ||
tomcat.*/** | ||
|
||
**/WEB-INF/classes/** |
24 changes: 0 additions & 24 deletions
24
app/src/main/java/com/techcourse/AppWebApplicationInitializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
.../java/nextstep/mvc/DispatcherServlet.java → ...ava/com/techcourse/DispatcherServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/techcourse/DispatcherServletInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.ServletContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import web.org.springframework.web.WebApplicationInitializer; | ||
|
||
/** | ||
* Base class for {@link WebApplicationInitializer} | ||
* implementations that register a {@link DispatcherServlet} in the servlet context. | ||
*/ | ||
public class DispatcherServletInitializer implements WebApplicationInitializer { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(DispatcherServletInitializer.class); | ||
|
||
private static final String DEFAULT_SERVLET_NAME = "dispatcher"; | ||
|
||
@Override | ||
public void onStartup(final ServletContext servletContext) { | ||
final var dispatcherServlet = new DispatcherServlet(); | ||
|
||
final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); | ||
if (registration == null) { | ||
throw new IllegalStateException("Failed to register servlet with name '" + DEFAULT_SERVLET_NAME + "'. " + | ||
"Check if there is another servlet registered under the same name."); | ||
} | ||
|
||
registration.setLoadOnStartup(1); | ||
registration.addMapping("/"); | ||
|
||
log.info("Start AppWebApplication Initializer"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.techcourse; | ||
|
||
import org.apache.catalina.Context; | ||
import org.apache.catalina.LifecycleException; | ||
import org.apache.catalina.connector.Connector; | ||
import org.apache.catalina.core.StandardContext; | ||
import org.apache.catalina.startup.Tomcat; | ||
import org.apache.tomcat.util.scan.StandardJarScanner; | ||
|
||
import java.io.File; | ||
|
||
public class TomcatStarter { | ||
|
||
public static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/"; | ||
|
||
private final Tomcat tomcat; | ||
|
||
public TomcatStarter(final int port) { | ||
this(WEBAPP_DIR_LOCATION, port); | ||
} | ||
|
||
public TomcatStarter(final String webappDirLocation, final int port) { | ||
this.tomcat = new Tomcat(); | ||
tomcat.setConnector(createConnector(port)); | ||
|
||
final var docBase = new File(webappDirLocation).getAbsolutePath(); | ||
final var context = (StandardContext) tomcat.addWebapp("", docBase); | ||
skipJarScan(context); | ||
skipClearReferences(context); | ||
} | ||
|
||
public void start() { | ||
try { | ||
tomcat.start(); | ||
} catch (LifecycleException e) { | ||
throw new UncheckedServletException(e); | ||
} | ||
} | ||
|
||
public void stop() { | ||
try { | ||
tomcat.stop(); | ||
tomcat.destroy(); | ||
} catch (LifecycleException e) { | ||
throw new UncheckedServletException(e); | ||
} | ||
} | ||
|
||
private Connector createConnector(final int port) { | ||
final var connector = new Connector(); | ||
connector.setPort(port); | ||
return connector; | ||
} | ||
|
||
private void skipJarScan(final Context context) { | ||
final var jarScanner = (StandardJarScanner) context.getJarScanner(); | ||
jarScanner.setScanClassPath(false); | ||
} | ||
|
||
private void skipClearReferences(final StandardContext context) { | ||
/** | ||
* https://tomcat.apache.org/tomcat-10.1-doc/config/context.html | ||
* | ||
* setClearReferencesObjectStreamClassCaches 번역 | ||
* true인 경우 웹 응용 프로그램이 중지되면 Tomcat은 직렬화에 사용되는 | ||
* ObjectStreamClass 클래스에서 웹 응용 프로그램에 의해 로드된 | ||
* 클래스에 대한 SoftReference를 찾고 찾은 모든 SoftReference를 지웁니다. | ||
* 이 기능은 리플렉션을 사용하여 SoftReference를 식별하므로 Java 9 이상에서 | ||
* 실행할 때 명령줄 옵션 -XaddExports:java.base/java.io=ALL-UNNAMED를 설정해야 합니다. | ||
* 지정하지 않으면 기본값인 true가 사용됩니다. | ||
* | ||
* ObjectStreamClass와 관련된 메모리 누수는 Java 19 이상, Java 17.0.4 이상 및 | ||
* Java 11.0.16 이상에서 수정되었습니다. | ||
* 수정 사항이 포함된 Java 버전에서 실행할 때 확인이 비활성화됩니다. | ||
* | ||
* Amazon Corretto-17.0.6은 경고 메시지가 나옴. | ||
* 학습과 관련 없는 메시지가 나오지 않도록 관련 설정을 끈다. | ||
*/ | ||
context.setClearReferencesObjectStreamClassCaches(false); | ||
context.setClearReferencesRmiTargets(false); | ||
context.setClearReferencesThreadLocals(false); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/techcourse/UncheckedServletException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.techcourse; | ||
|
||
public class UncheckedServletException extends RuntimeException { | ||
|
||
public UncheckedServletException(Exception e) { | ||
super(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/nextstep/web/annotation/Controller.java → ...pringframework/stereotype/Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.