• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

15 April 2016

JAVA servlet MVC2 WEB

Configuration

WebContent/WEB-INF/config.properties
/bbs.do=com.mvc.bbs.BoardAction
/guest.do=com.mvc.guest.GuestAction
cs
WebContent/WEB-INF/web.xml
<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>
cs

CONTROLER

com.mvc.MyServlet.java  
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);
        }
    }
}
cs

MODEL

com.mvc.MyAction.java
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);
    }
}
cs
com.mvc.BoardAction.java
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");
        }
    }
}
cs
com.mvc.GuestAction.java
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");
        }
    }
}
cs

VIEW

root/mvc/bbs/list.jsp
<body>
    <a href="<%=cp%>/bbs.do">게시판</a>
    <a href="<%=cp%>/guest.do">방명록</a><br/>
    게시판 리스트입니다.<br/>
    <a href="<%=cp%>/bbs.do?mode=created">글쓰기</a>
</body>
cs
root/mvc/guest/created.jsp
<body>
    <a href="<%=cp%>/bbs.do">게시판</a>
    <a href="<%=cp%>/guest.do">방명록</a><br/>
    게시판 글쓰기입니다.<br/>
</body>
cs
root/mvc/guest/guest.jsp
<body>
    <a href="<%=cp%>/bbs.do">게시판</a>
    <a href="<%=cp%>/guest.do">방명록</a><br/>
    방명록입니다.<br/>
</body>
cs

 

BACK TO TOP

Xuống cuối trang