JSP - ServletContainerInitializer
ServletContainerInitializer¿¡ ´ëÇØ
¶óÀ̺귯¸® ÇüÅÂ(jar)·Î ÆÐŰ¡Çؼ À¥ ¾ÖÇø®ÄÉÀ̼ÇÀÇ lib¿¡ ¹èÆ÷Çϸé À¥°´Ã¼¸¦(¼ºí¸´, ÇÊÅÍ, ¸®½º³Ê) µî·ÏÇÏ´Â °ÍÀÌ °¡´ÉÇÏ´Ù. °øÅëÀûÀ¸·Î »ç¿ëµÇ´Â ¸ðµâÀ» ¶óÀ̺귯¸® ÇüÅ·Π°³¹ßÇÏ´Â°Ô °¡´ÉÇÏ°Ô µÇ¾ú´Ù.
ServletContainerInitializerÀÇ ¼³Á¤
ServletContainerInitializer ÀÎÅÍÆäÀ̽º¸¦ implementÇϸé onStartup¸Þ¼Òµå¸¦ ±¸ÇöÇØ ÁÖ¾î¾ßÇϴµ¥ ¿©±â¼ À¥°´Ã¼¸¦ µî·ÏÇÏ°Ô µÈ´Ù.
- Servlet : ServletRegistration
- Filter : FilterRegistration
- Listener : ServletContextÀÇ ÀνºÅϽº¿¡ addListener·Î µî·ÏÇϸéµÈ´Ù.
¼ºí¸´
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.goodcodes.plugin; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PluggableServlet extends HttpServlet { private static final long serialVersionUID = -3465964380408959525L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html" ); PrintWriter writer = response.getWriter(); writer.println( "<h3>Pluggable servlet</h3>" ); } } |
ServletContainerInitializer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.goodcodes.plugin; import java.util.Set; import javax.servlet.FilterRegistration; import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import javax.servlet.annotation.HandlesTypes; @HandlesTypes ({PluggableServlet. class }) public class PlugIn implements ServletContainerInitializer { @Override public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException { System.out.println( "onStartup called. /pluginÆú´õ¿¡ ¼ºí¸´À» µî·ÏÇÕ´Ï´Ù." ); ServletRegistration registration = servletContext.addServlet( "pluggableServlet" , "com.goodcodes.plugin.PluggableServlet" ); registration.addMapping( "/plugin" ); System.out.println( "Á¤»óÀûÀ¸·Î µî·ÏÇÏ¿´½À´Ï´Ù." ); } } |
javax.servlet.ServletContainerInitializer ÆÄÀÏ
ServletContainerInitializer À» ±¸ÇöÇÑ Å¬·¡½ºÀÇ Á¤º¸¸¦ ÄÚµùÇÑ´Ù.
1 | com.goodcodes.plugin.PlugIn |
Ç÷¯±×ÀÎ ±¸Á¶
META-INF/servicesÆú´õ¿¡ javax.servlet.ServletContainerInitializer¶ó´Â ÆÄÀÏÀ» ÀÛ¼ºÇÑ´Ù.
À¥ ¾îÇø®ÄÉÀ̼ÇÀÇ lib¿¡ Ãß°¡ÇÏ°í À¥¼¹ö°¡ ±âµ¿µÇ¸é Ãß°¡ÇÑ À¥°´Ã¼°¡ Ç÷¯±×ÀεǼ ÀÛµ¿ÇÑ´Ù.
Ãâó: http://goodcodes.tistory.com/entry/JSP-ServletContainerInitializer [Good Codes Make a Good Program]