본문 바로가기
Web Programming/JSP

[JSP] FrontController Pattern & Command Pattern

FrontController Pattern & Command Pattern





url-pattern


서블릿 매핑 하는 이유는 경로가 너무 길고 보안상 문제 때문에 한다.


디렉터리 패턴

디렉터리 형태로 서버의 해당 컴포넌트(서블릿)를 찾아서 실행하는 구조


http://localhost:8181/jsp_21_1_ex1_memberex/Hello       →Hello 서블릿

http://localhost:8181/jsp_21_1_ex1_memberex/World      World 서블릿


각 디렉터리 매핑명으로 별개의 서블릿으로 찾아간다.


확장자 패턴

확장자 형태로 서버의 해당 컴포넌트를 찾아서 실행하는 구조


http://localhost:8181/jsp_21_1_ex1_memberex/Hello.do

http://localhost:8181/jsp_21_1_ex1_memberex/World.do


→*.do 서블릿


do로 매핑되어있는 같은 서블릿으로 간다.

그 후, 서블릿 내에서 hellow와 world를 구분한다.




FrontController - pattern


클라이언트의 다양한 요청을 한곳으로 집중시켜, 새발 및 유지보수에 효율성을 극대화 한다.





frontControllerEx.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ 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>
 
    <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>
cs


모든 확장자가 .do로 끝난다.



FrontCon.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
 */
@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);
        String command = uri.substring(conPath.length());
        System.out.println("command : " + command);
 
        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("----------------");
        }
    }
 
}
 
cs



 






Command - pattern


클라이언트로부터 받은 요청들에 대해서, 서블릿이 작업을 직접처리 하지 않고, 해당 클래스가 처리 하도록 한다.



서블릿이 모든 처리를 맡게 된다면, 서블릿의 코드 또한 방대해지게 된다. 그렇기 때문에 또 다른 서블릿에 보내버린다.


'Web Programming > JSP' 카테고리의 다른 글

[JSP] Mapping  (0) 2018.07.10
[JSP] Servlet  (0) 2018.07.10
[JSP] JDBC  (5) 2018.05.01
[JSP] DAO DTO  (1) 2018.05.01
[JSP] 자바빈을 이용한 회원가입 만들기  (4) 2018.04.16