JSP| 💔FrontControllerモードとCommandモード


  • カタログモード:独立したテンプレートを探す/어쩌구
  • 拡張モード:無条件*.do
  • Front Controlモード


  • 全ての要求を処理するテンプレートの作成>重複を解消し、集中開発
  • Front Controlを使用したファイル
  • <%@ page language="java" contentType="text/html; charset=EUC-KR"
    	pageEncoding="EUC-KR"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    
    	<!-- insert누르면 insert.do 요청 -->
    	<a href="insert.do">insert</a>
    	<hr />
    	<a href="http://localhost:8181<%=request.getContextPath()%>/update.do">update</a>
    	<hr />
    	<a href="http://localhost:8181/jsp_25_2_ex1_frontex/select.do">select</a>
    	<hr />
    	<a href="<%=request.getContextPath()%>/delete.do">delete</a>
    
    </body>
    </html>
  • FrontController
  • package com.javalec.ex;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class FrontCon
     */
    //확장자 패턴 *.do
    @WebServlet("*.do")
    public class FrontCon extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public FrontCon() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("doGet");
    		actionDo(request, response);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("doPost");
    		actionDo(request, response);
    	}
    
    	private void actionDo(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("actionDo");
    
    		String uri = request.getRequestURI();
    		System.out.println("uri : " + uri);
    		String conPath = request.getContextPath();
    		System.out.println("conPath : " + conPath);
    
    		// conPath의 길이만큼 uri를 자름 > 뒤에 요청한 파일의 이름 (.do)만 남음
    		String command = uri.substring(conPath.length());
    		System.out.println("command : " + command);
    
    		// 뒤에 요청한 파일의 이름 (.do)만 남음 > 값 확인 > 모든 일 한 곳에서 처리
    		if (command.equals("/insert.do")) {
    			System.out.println("insert");
    			System.out.println("----------------");
    		} else if (command.equals("/update.do")) {
    			System.out.println("update");
    			System.out.println("----------------");
    		} else if (command.equals("/select.do")) {
    			System.out.println("select");
    			System.out.println("----------------");
    		} else if (command.equals("/delete.do")) {
    			System.out.println("delete");
    			System.out.println("----------------");
    		}
    	}
    
    }
    

    Commandモード



    1.1つのサーブレットからすべてのリクエストを受信
    2.サーブレットをインタフェースとする別のクラスでの処理
    3.DAOを使用して論理にアクセスし、実行します.
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
    	pageEncoding="EUC-KR"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    	<!-- @WebServlet("*.do")으로 FrontController가 받는다 -->
    	<a href="membersAll.do">전체 회원 정보 조회</a>
    
    </body>
    </html>
    package com.javalec.ex;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Timestamp;
    import java.util.ArrayList;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class FrontCon
     */
    @WebServlet("*.do")
    public class FrontCon extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FrontCon() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("doGet");
    		actionDo(request, response);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("doPost");
    		actionDo(request, response);
    	}
    	
    	private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("actionDo");
    		
    		String uri = request.getRequestURI();
    		String conPath = request.getContextPath();
    		String command = uri.substring(conPath.length());
    		
    		if(command.equals("/membersAll.do")) {
    			response.setContentType("text/html; charset=EUC-KR");
    			PrintWriter writer = response.getWriter();
    			writer.println("<html><head></head><body>");
    			
    			//작업 넘기기
    			Service service = new MembersAllService();
    			ArrayList<MemberDto> dtos = service.execute(request, response);
    			
    			for (int i = 0; i < dtos.size(); i++) {
    				MemberDto dto = dtos.get(i);
    				String id = dto.getId();
    				String pw = dto.getPw();
    				String name = dto.getName();
    				String eMail = dto.geteMail();
    				Timestamp rDate = dto.getrDate();
    				String address = dto.getAddress();
    				
    				writer.println(id + ", " + pw + ", " + name + ", " + eMail + ", " + rDate.toLocalDateTime() + ", " + address + "<hr />");
    			}
    			
    			writer.println("</body></html>");
    		}
    		
    	}
    
    }