- 회원 등록/수정/삭제/조회가 가능한 웹서비스
1. study_db - client table 생성
1 2 3 4 5 6 7 8 9 | create table client( clientId varchar (50) not null , clientPassword varchar (100) not null , clientName varchar (30) not null , clientEmail varchar (100) not null , clientPhone varchar (100), clientAddress varchar (100), primary key (clientId) ) |
2. 홈화면

1) WebContent - index.jsp
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >model2</ title > </ head > < body > < h3 >Model2 Architecture</ h3 > < p >Model2 Controller(servlet)을 통해 Business로직과 view를 분리 </ p > < img src = "model2.gif" /> < h3 >메뉴</ h3 > < p >Model2를 이용해 회원관리하고, 회원만 게시판을 이용할 수 있게함.</ p > <!-- 아래의 링크(#)를 채워 나갑니다 --> < table border = "1" > < tr > < td rowspan = "4" >회원관리</ td > < td >< a href = "#" >회원등록</ a ></ td > <!-- 로그인 후에는 감추기--> </ tr > < tr > < td >< a href = "#" >회원수정</ a ></ td > <!-- 로그인시 보이게, 로그아웃시에 안보이게 --> </ tr > < tr > < td >< a href = "#" >회원삭제</ a ></ td > <!-- 로그인시 보이게, 로그아웃시에 안보이게 --> </ tr > < tr > < td >< a href = "#" >회원조회</ a ></ td > </ tr > < tr > < td rowspan = "2" >회원전용</ td > < td >< a href = "#" >로그인</ a ></ td > <!-- 로그인 후에는 로그아웃으로 변경 --> </ tr > < tr > < td >< a href = "#" >게시판</ a ></ td > <!-- 로그인 안하면 alert --> </ tr > </ table > </ body > </ html > |
- 12번째 줄 이미지

-15번째 줄부터 채워나가면 됨.
3. Controller
2) src - loader.DbDriverLoader.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 | package loader; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class DbDriverLoader implements ServletContextListener { public DbDriverLoader() { } public void contextDestroyed(ServletContextEvent arg0) { } public void contextInitialized(ServletContextEvent arg0) { try { Class.forName( "org.mariadb.jdbc.Driver" ); System.out.println( "db driver loading..." ); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } |
3) src - controller.DispatcherServlet.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 | package controller; 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; @WebServlet ( "/DispatcherServlet" ) public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1L; public DispatcherServlet() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { handleRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "utf-8" ); handleRequest(request, response); } private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try { // 클라이언트의 요청(command)를 확인 -> HandlerMapping을 통해 구현체를 반환받아 실행 String command=request.getParameter( "command" ); // 리턴 정보는 1.이동할 view와 2.이동방식(forward/redirect) String url=HandlerMapping.getInstance().create(command).execute(request,response); // 리턴 방식에 따라 클라이언트에게 이동되도록 처리 if (url.trim().startsWith( "redirect:" )) response.sendRedirect(url.trim().substring( 9 )); else request.getRequestDispatcher(url).forward(request, response); } catch (Exception e) { //Exception 발생 시 error.jsp 페이지로 응답 처리 e.printStackTrace(); response.sendRedirect( "error.jsp" ); } } } |
4) src - controller.HandlerMapping.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package controller; /* * 컨트롤러 구현체 생성을 전담하는 클래스 * DispatcherServlet과 Controller 계열 구현체들과 결합도를 낮춤 */ public class HandlerMapping { private static HandlerMapping instance= new HandlerMapping(); private HandlerMapping() {} public static HandlerMapping getInstance() { return instance; } public Controller create(String command) { Controller c= null ; //여기서부터 처리할 command와 실제 Controller Mapping return c; } } |
5) src - controller.Controller.java
1 2 3 4 5 6 7 8 9 10 11 12 | package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /* * 실제 컨트롤러 업무를 수행하는 구상클래스(구현체)들이 implements 하는 인터페이스 * DispatcherServlet은 실제 컨트롤러 구현체의 메소드를 확인하지 않아도 상위 Controller 인터페이스만 보고 사용 가능. */ public interface Controller { public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception; } |
4. Model
1) src - model.ClientDAO
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 | package model; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class ClientDAO { private static ClientDAO instance= new ClientDAO(); private ClientDAO() {} public static ClientDAO getInstance() { return instance; } public Connection getConnection() throws SQLException{ } public void closeAll(PreparedStatement pstmt,Connection con) throws SQLException{ if (pstmt!= null ) pstmt.close(); if (con!= null ) con.close(); } public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con) throws SQLException{ if (rs!= null ) rs.close(); closeAll(pstmt, con); } //여기서부터 필요한 메소드 추가 } |
2) src - model.ClientDTO
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 | package model; public class ClientDTO { private String clientId; private String clientPassword; private String clientName; private String clientEmail; private String clientPhone; private String clientAddress; public ClientDTO() { super (); } public ClientDTO(String clientId, String clientPassword, String clientName, String clientEmail, String clientPhone, String clientAddress) { super (); this .clientId = clientId; this .clientPassword = clientPassword; this .clientName = clientName; this .clientEmail = clientEmail; this .clientPhone = clientPhone; this .clientAddress = clientAddress; } public String getClientId() { return clientId; } public void setClientId(String clientId) { this .clientId = clientId; } public String getClientPassword() { return clientPassword; } public void setClientPassword(String clientPassword) { this .clientPassword = clientPassword; } public String getClientName() { return clientName; } public void setClientName(String clientName) { this .clientName = clientName; } public String getClientEmail() { return clientEmail; } public void setClientEmail(String clientEmail) { this .clientEmail = clientEmail; } public String getClientPhone() { return clientPhone; } public void setClientPhone(String clientPhone) { this .clientPhone = clientPhone; } public String getClientAddress() { return clientAddress; } public void setClientAddress(String clientAddress) { this .clientAddress = clientAddress; } @Override public String toString() { return "ClientDTO [clientId=" + clientId + ", clientPassword=" + clientPassword + ", clientName=" + clientName + ", clientEmail=" + clientEmail + ", clientPhone=" + clientPhone + ", clientAddress=" + clientAddress + "]" ; } } |
5. 에러페이지
1) WebContent - error.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Insert title here</ title > </ head > < body > < img src = "return.jpeg" />< br > < p >Console 확인 < a href = "index.jsp" >돌아가기</ a ></ p > </ body > </ html > |
- 10째 줄 이미지

[참고] 모델2 개념 참고 https://thatisgood.tistory.com/entry/model1-vs-model2-%EC%B0%A8%EC%9D%B4%EC%A0%90
'1.JAVA Web Programming > [3주차]JSP+model2' 카테고리의 다른 글
Model2 Architecture - (5) 회원수정 (0) | 2019.08.03 |
---|---|
Model2 Architecture - (4) 로그아웃 (0) | 2019.08.03 |
Model2 Architecture - (3) 로그인 (0) | 2019.08.02 |
Model2 Architecture - (2) 회원조회 (0) | 2019.08.01 |
Model2 Architecture - (1) 회원가입 (0) | 2019.08.01 |