Last active
          June 26, 2022 08:30 
        
      - 
      
 - 
        
Save antop-dev/746d443f96d1b2a3e2ec4bddc305cab6 to your computer and use it in GitHub Desktop.  
    템플릿 메소드 패턴 - 스프링의 DispatcherServlet
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | package org.antop.billiardslove; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| public class DispatcherServlet extends FrameworkServlet { | |
| @Override | |
| protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception { | |
| // implements | |
| } | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | import org.springframework.web.util.NestedServletException; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| public abstract class FrameworkServlet extends HttpServlet { | |
| // HttpServlet.doGet 메소드(후크)를 오버라이드 | |
| @Override | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| processRequest(request, response); | |
| } | |
| // 템플릿 메소드 | |
| protected final void processRequest(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| try { | |
| doService(request, response); | |
| } catch (ServletException | IOException ex) { | |
| throw ex; | |
| } catch (Throwable ex) { | |
| throw new NestedServletException("Request processing failed", ex); | |
| } | |
| } | |
| // 하위 클래스가 구현해야 하는 추상 메소드 | |
| protected abstract void doService(HttpServletRequest request, HttpServletResponse response) | |
| throws Exception; | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| import java.util.ResourceBundle; | |
| public abstract class HttpServlet { | |
| private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings"; | |
| private static final ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE); | |
| // 후크 메소드 (FrameworkServlet가 오버라이드 함) | |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
| throws ServletException, IOException { | |
| String msg = lStrings.getString("http.method_get_not_supported"); | |
| sendMethodNotAllowed(req, resp, msg); | |
| } | |
| // 후크 메소드 (FrameworkServlet가 오버라이드 함) | |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
| throws ServletException, IOException { | |
| // 위와 같음 | |
| } | |
| // 다른 후크 메소드 | |
| // doHead() | |
| // doDelete() | |
| // doPut() | |
| private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse resp, String msg) throws IOException { | |
| // 에러를 응답한다. | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment