2013. 7. 29. 11:42

목차

■ config.properties
■ web.xml
■ CONTROL
■ MODEL
■ VIEW



■ WebContent/WEB-INF/config.properties

/bbs.do=com.mvc.bbs.BoardAction

/guest.do=com.mvc.guest.GuestAction



 WebContent/WEB-INF/web.xml

1
2
3
4
5
6
7
8
<servlet>
   <servlet-name>my</servlet-name>
   <servlet-class>com.mvc.MyServlet</servlet-class>
   <init-param>
       <param-name>config</param-name>
       <param-value>/WEB-INF/config.properties</param-value>
   </init-param>
 </servlet>



■ CONTROL

● com.mvc.MyServlet.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
72
73
74
75
76
77
78
79
80
81
82
public class MyServlet extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    private Map<String, Object> map = new Hashtable<String, Object>();
 
    public void init(ServletConfig config) throws ServletException {
 
        super.init(config);
 
        ServletContext context = getServletContext();
        // web.xml 의 servlet 태그안의 init-param 값 읽어오기
        String pathname = config.getInitParameter("config");
 
        if (pathname == null)
            return;
 
        pathname = context.getRealPath(pathname);
 
        try {
            // properties 파일에 저장된 내용을 Properties 객체에 저장
            Properties prop = new Properties();
            FileInputStream fis = null;
 
            fis = new FileInputStream(pathname);
 
            // 파일의 내용을 읽어 Properties 객체에 저장
            prop.load(fis);
            fis.close();
 
            // Properties 객체에 저장된 클래스의 객체를 생성 map에 저장
            Iterator<Object> it = prop.keySet().iterator();
 
            while (it.hasNext()) {
                String key = (String) it.next();
                String className = prop.getProperty(key);
 
                Class<?> cls = Class.forName(className);
                Object ob = cls.newInstance();
 
                map.put(key, ob);
            }
 
        } catch (Exception e) {
            System.out.println(e);
        }
    }
 
    public void destroy() {
    }
 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        process(req, resp);
    }
 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        process(req, resp);
    }
 
    protected void process(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        MyAction action = null;
 
        try {
            String uri = req.getRequestURI();
 
            if (uri.indexOf(req.getContextPath()) == 0) {
                uri = uri.substring(req.getContextPath().length());
                action = (MyAction) map.get(uri);
                action.execute(req, resp);
            }
 
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}



■ MODEL

● com.mvc.MyAction.java

1
2
3
4
5
6
7
8
9
10
11
12
public abstract class MyAction {
 
    public abstract void execute(HttpServletRequest req,
            HttpServletResponse resp) throws ServletException, IOException;
 
    public void forward(HttpServletRequest req, HttpServletResponse resp,
            String path) throws ServletException, IOException {
 
        RequestDispatcher rd = req.getRequestDispatcher(path);
        rd.forward(req, resp);
    }
}


● com.mvc.BoardAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
public class BoardAction extends MyAction {
    public void execute(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        String mode = req.getParameter("mode");
 
        if (mode == null || mode.equals("list")) {
            forward(req, resp, "/mvc/bbs/list.jsp");
        } else if (mode.equals("created")) {
            forward(req, resp, "/mvc/bbs/created.jsp");
        }
    }
}


● com.mvc.GuestAction.java

1
2
3
4
5
6
7
8
9
10
11
12
public class GuestAction extends MyAction {
    public void execute(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        String mode = req.getParameter("mode");
 
        if (mode == null || mode.equals("guest")) {
 
            forward(req, resp, "/mvc/guest/guest.jsp");
        }
    }
}



 VIEW

● root/mvc/bbs/list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<body>
 
<a href="<%=cp%>/bbs.do">게시판</a>
<a href="<%=cp%>/guest.do">방명록</a><br/>
 
게시판 리스트입니다.<br/>
<a href="<%=cp%>/bbs.do?mode=created">글쓰기</a>
 
</body>
 
 
root/mvc/bbs/created.jsp
<body>
 
<a href="<%=cp%>/bbs.do">게시판</a>
<a href="<%=cp%>/guest.do">방명록</a><br/>
게시판 글쓰기입니다.<br/>
 
</body>


● root/mvc/guest/guest.jsp

1
2
3
4
5
6
7
<body>
 
<a href="<%=cp%>/bbs.do">게시판</a>
<a href="<%=cp%>/guest.do">방명록</a><br/>
방명록입니다.<br/>
 
</body>



'JSP > JspServlet' 카테고리의 다른 글

JSP 개요  (0) 2013.07.29
연산자 검색  (0) 2013.07.29
달력  (0) 2013.07.29
getParameterValues 처리  (0) 2013.07.25
아파치 톰캣 연동 & 이유  (0) 2013.07.25
Posted by 1+1은?