diff --git a/springboot/server/src/main/java/hello/container/AppInit.java b/springboot/server/src/main/java/hello/container/AppInit.java new file mode 100644 index 00000000..70d9380f --- /dev/null +++ b/springboot/server/src/main/java/hello/container/AppInit.java @@ -0,0 +1,7 @@ +package hello.container; + +import jakarta.servlet.ServletContext; + +public interface AppInit { + void onStartup(ServletContext servletContext); +} diff --git a/springboot/server/src/main/java/hello/container/AppInitV1Servlet.java b/springboot/server/src/main/java/hello/container/AppInitV1Servlet.java new file mode 100644 index 00000000..3d23f2e7 --- /dev/null +++ b/springboot/server/src/main/java/hello/container/AppInitV1Servlet.java @@ -0,0 +1,18 @@ +package hello.container; + +import hello.servlet.HelloServlet; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletRegistration; + +public class AppInitV1Servlet implements AppInit { + @Override + public void onStartup(ServletContext servletContext) { + System.out.println("AppInitV1Servlet.onStartup"); + + // 서블릿 코드 등록 + ServletRegistration.Dynamic helloServlet = + servletContext.addServlet("helloServlet", new HelloServlet()); + + helloServlet.addMapping("/hello-servlet"); + } +} diff --git a/springboot/server/src/main/java/hello/container/MyContainerInitV1.java b/springboot/server/src/main/java/hello/container/MyContainerInitV1.java new file mode 100644 index 00000000..3fa9b359 --- /dev/null +++ b/springboot/server/src/main/java/hello/container/MyContainerInitV1.java @@ -0,0 +1,16 @@ +package hello.container; + +import jakarta.servlet.ServletContainerInitializer; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; + +import java.util.Set; + +public class MyContainerInitV1 implements ServletContainerInitializer { + @Override + public void onStartup(Set> c, ServletContext ctx) throws ServletException { + System.out.println("MyContainer.onStartup"); + System.out.println("c = " + c); + System.out.println("ctx = " + ctx); + } +} diff --git a/springboot/server/src/main/java/hello/container/MyContainerInitV2.java b/springboot/server/src/main/java/hello/container/MyContainerInitV2.java new file mode 100644 index 00000000..cfecf3f3 --- /dev/null +++ b/springboot/server/src/main/java/hello/container/MyContainerInitV2.java @@ -0,0 +1,27 @@ +package hello.container; + +import jakarta.servlet.ServletContainerInitializer; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.HandlesTypes; + +import java.util.Set; + +@HandlesTypes(AppInit.class) +public class MyContainerInitV2 implements ServletContainerInitializer { + @Override + public void onStartup(Set> c, ServletContext ctx) throws ServletException { + System.out.println("MyContainerInitV2.onStartup"); + System.out.println("c = " + c); + + + for (Class appInitClass : c) { + try { + AppInit appInit = (AppInit) appInitClass.getDeclaredConstructor().newInstance(); + appInit.onStartup(ctx); + } catch (Exception e) { + throw new RuntimeException(); + } + } + } +} diff --git a/springboot/server/src/main/java/hello/servlet/HelloServlet.java b/springboot/server/src/main/java/hello/servlet/HelloServlet.java new file mode 100644 index 00000000..70a1c773 --- /dev/null +++ b/springboot/server/src/main/java/hello/servlet/HelloServlet.java @@ -0,0 +1,16 @@ +package hello.servlet; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +public class HelloServlet extends HttpServlet { + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + System.out.println("HelloServlet.service"); + resp.getWriter().println("hello servlet!"); + } +} diff --git a/springboot/server/src/main/resources/META-INF/services/jakarta.servlet.ServletContainerInitializer b/springboot/server/src/main/resources/META-INF/services/jakarta.servlet.ServletContainerInitializer new file mode 100644 index 00000000..28220f13 --- /dev/null +++ b/springboot/server/src/main/resources/META-INF/services/jakarta.servlet.ServletContainerInitializer @@ -0,0 +1,2 @@ +hello.container.MyContainerInitV1 +hello.container.MyContainerInitV2 \ No newline at end of file