이경화 Ch13. 웹 프로그래밍 설계 및 실습 MVC2 Member
First off [ 영문 API_1.6] [ 한글 API_1.6] [HTML 4.01 / XHTML 1.0 Reference] [The JavaBeans™ Tutorial] Servlet and JSP, C.Edward Chow
Contents Basic MVC2 Pattern JSP Model 2 architecture - Member 3
Column Add in Table (1/2)
Column Add in Table (2/2) ##> mysql -uroot -p0000 mysql> use lkh mysql> show tables; mysql> alter table member add regdate date; mysql> update member set regdate = now(); mysql> desc member; mysql> select * from member;
프로젝트 구조 Context_root : 이니셜 _PRJ Java Source –DB 연결 : mvc2.db.DBConn.java –DAO : mvc2.member.dao.MemberDAO.java –DTO : mvc2.member.dto.MemberVO.java –Servlet : mvc2.member.controller.MemberServlet.java JSP Source – 입력 폼 : inputMember.jsp – 회원 목록 : listMember.jsp – 회원 상세 : readMember.jsp
MVC1 member 동작 과정 7 createMember.jsp createProc.jsp listMember.jsp readMember.jsp updateProc.jsp deleteProc.jsp DAO.java DTO.java DB DBConnection.java update delete list create read VIEW MODEL
MVC2 member 동작 과정 8 createMember.jsp MemberServlet.java listMember.jsp main.jsp readMember.jsp DAO.java DTO.java DB DBConnection.java list create read update / delete VIEW CONTROLLER MODEL
Model 9 - DB 연결 : mvc2.db.DBConn.java - DAO : mvc2.member.dao.MemberDAO.java - DTO : mvc2.member.dto.MemberVO.java
package mvc2.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBConn { DBConn.java
/** * Mysql DB 연결 Connection */ public static Connection mysqlConn() { Connection conn = null; String url= "jdbc:mysql:// :3306/lkh"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url,"root","0000"); } catch(Exception e) { System.out.println(e.toString()); } return conn; } DBConn.java
/** * Mysql DB 연결 해제 conn */ public static void close(Connection conn) { try { if(conn !=null) conn.close(); } catch(Throwable e) { System.out.println(e.toString()); } finally { try { if(conn != null) conn.close(); } catch ( Exception e ) { System.out.println(e.toString()); }
/** * Mysql DB 연결 해제 conn pstmt */ public static void close(Connection conn, PreparedStatement pstmt) { try { if(pstmt !=null) pstmt.close(); if(conn !=null) conn.close(); } catch(Throwable e) { System.out.println(e.toString()); } finally { try { if(pstmt != null) pstmt.close(); if(conn != null) conn.close(); } catch ( Exception e ) { System.out.println(e.toString()); }
/** * Mysql DB 연결 해제 conn pstmt rs */ public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) { try { if(rs != null ) rs.close(); if(pstmt !=null) pstmt.close(); if(conn !=null) conn.close(); } catch(Throwable e) { System.out.println(e.toString()); } finally { try { if(rs != null ) rs.close(); if(pstmt != null) pstmt.close(); if(conn != null) conn.close(); } catch ( Exception e ) { } } // end of class
package mvc2.member.dto; public class MemberVO { private int seq = 0; private String userid = ""; private String username = ""; private String userpw = ""; private String regdate = ""; public int getSeq() { return seq; } public String getUserid() { return userid; } public String getUsername() { return username; } public String getUserpw() { return userpw; } public String getRegdate() { return regdate; } public void setSeq(int seq) { this.seq = seq; } public void setUserid(String userid) { this.userid = userid; } public void setUsername(String username) { this.username = username; } public void setUserpw(String userpw) { this.userpw = userpw; } public void setRegdate(String regdate) { this.regdate = regdate; } } MemberVO.java
package mvc2.member.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.*; import demoPkg.DBConn; import mvc2.member.dto.MemberVO; public class MemberDAO { MemberDAO.java
/** * 회원 정보 생성 dto boolean */ public boolean create (MemberVO dto) { Connection conn = null; PreparedStatement pstmt = null; String insertSQL = "INSERT INTO member(userid, username, userpw, regdate) VALUES(?,?,?,now())"; int res = 0; try { conn = DBConn.mysqlConn(); pstmt = conn.prepareStatement(insertSQL); pstmt.setString(1, dto.getUserid()); pstmt.setString(2, dto.getUsername()); pstmt.setString(3, dto.getUserpw()); res = pstmt.executeUpdate(); } catch (Exception e) { System.out.println(e.getMessage());
} finally { try { DBConn.close(conn, pstmt); } catch (Exception e) { e.printStackTrace(); } if (res > 0) return true; else return false; }
/** * 회원 목록 보기 Vector */ public Vector list() { Vector v1 = new Vector(); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String listSQL = "SELECT seq, userid, username, userpw, regdate from MEMBER"; try { conn = DBConn.mysqlConn(); pstmt = conn.prepareStatement(listSQL); rs = pstmt.executeQuery(); while(rs.next()) { MemberVO dto = new MemberVO(); dto.setSeq(rs.getInt("seq")); dto.setUserid(rs.getString("userid")); dto.setUsername(rs.getString("username")); dto.setUserpw(rs.getString("userpw")); dto.setRegdate(rs.getString("regdate"));
v1.addElement(dto); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { try { DBConn.close(conn, pstmt, rs); } catch (Exception e) { e.printStackTrace(); } return v1; }
/** * 회원정보 상세보기 idVal MemberVO */ public MemberVO read (int seqVal) { MemberVO dto = new MemberVO(); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String readSQL = "SELECT userid, username, userpw, regdate FROM member WHERE seq=?"; try { conn = DBConn.mysqlConn(); pstmt = conn.prepareStatement(readSQL); pstmt.setInt(1, seqVal); rs = pstmt.executeQuery(); if(rs.next()) { dto.setSeq(seqVal); dto.setUserid(rs.getString("userid")); dto.setUsername(rs.getString("username")); dto.setUserpw(rs.getString("userpw")); dto.setRegdate(rs.getString("regdate")); }
} catch (Exception e) { System.out.println(e.getMessage()); } finally { try { DBConn.close(conn, pstmt, rs); } catch (Exception e) { e.printStackTrace(); } return dto; }
/** * 회원 정보 수정 dto boolean */ public boolean update (MemberVO dto) { Connection conn = null; PreparedStatement pstmt = null; String updateSQL = "UPDATE member SET userid=?, username=?, userpw=?, regdate=now() WHERE seq=?"; int res = 0; try { conn = DBConn.mysqlConn(); pstmt = conn.prepareStatement(updateSQL); pstmt.setString(1, dto.getUserid()); pstmt.setString(2, dto.getUsername()); pstmt.setString(3, dto.getUserpw()); pstmt.setInt(4, dto.getSeq()); res = pstmt.executeUpdate(); } catch (Exception e) { System.out.println(e.getMessage());
} finally { try { DBConn.close(conn, pstmt); } catch (Exception e) { e.printStackTrace(); } if (res > 0) return true; else return false; }
/** * 회원정보 삭제 dto boolean */ public boolean delete (int seqVal) { Connection conn = null; PreparedStatement pstmt = null; String deleteSQL = "DELETE FROM member WHERE seq=?"; int res = 0; try { conn = DBConn.mysqlConn(); pstmt = conn.prepareStatement(deleteSQL); pstmt.setInt(1, seqVal); res = pstmt.executeUpdate(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { try { DBConn.close(conn, pstmt);
} catch (Exception e) { e.printStackTrace(); } if (res > 0) return true; else return false; }
Controller 27 없는 경우 : Controller 선택 > 마우스우클릭 >other>Web>servlet 선택 MemberServlet mvc2.member.controller
package mvc2.member.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.RequestDispatcher; import mvc2.member.dao.MemberDAO; import mvc2.member.dto.MemberVO; import java.util.Vector; /** * Servlet implementation class MemberServlet */ public class MemberServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** HttpServlet#HttpServlet() */ public MemberServlet() { super(); } MemberServlet.java 자동생성 MVC2 방식 28
/** HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.myCall(request, response); } /** HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.myCall(request, response); } 29
public void myCall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("euc-kr"); response.setContentType("text/html; charset=euc-kr"); LoginAction dao = new LoginAction(); MemberVO dto = new MemberVO(); String url = ""; Boolean flag = false; String action = request.getParameter("actionType"); //System.out.println("action : "+ action); if (action.equals("list")) { Vector v1 = dao.list(); request.setAttribute("v1", v1); url = "/Demo8/listMember.jsp"; RequestDispatcher dispatcher =request.getRequestDispatcher(url); dispatcher.forward(request, response); 30
} else if (action.equals("create")) { dto.setUserid(request.getParameter("userid")); dto.setUsername(request.getParameter("username")); dto.setUserpw(request.getParameter("userpw")); flag = dao.create(dto); if(flag == true) { Vector v1 = dao.list(); request.setAttribute("v1", v1); url = "/Demo8/listMember.jsp"; } else { url = "/Demo8/inputMember.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(url); dispatcher.forward(request, response); } 31
<% boolean flag = dao.create(dto); if(flag == true) { response.sendRedirect(“listMember.jsp”); } else { response.sendRedirect(“inputMember.jsp”); } %> createProc.jsp
else if (action.equals("read")) { int seq = Integer.parseInt(request.getParameter("seq")); dto = dao.read(seq); request.setAttribute("dto", dto); url = "/Demo8/readMember.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(url); dispatcher.forward(request, response); } 33
else if (action.equals("update")) { int seq = Integer.parseInt(request.getParameter("seq")); dto.setSeq(seq); dto.setUserid(request.getParameter("userid")); dto.setUsername(request.getParameter("username")); dto.setUserpw(request.getParameter("userpw")); flag = dao.update(dto); if(flag == true) { Vector v1 = dao.list(); request.setAttribute("v1", v1); url = "/Demo8/listMember.jsp"; } else { dto = dao.read(seq); url = "/Demo8/readMember.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(url); dispatcher.forward(request, response); } 34
<% boolean flag = dao.update(dto); if(flag == true) { String url = "listMember.jsp"; response.sendRedirect(url); } else { String url = "readMember.jsp?seq="+dto.getSeq(); response.sendRedirect(response.encodeRedirectURL(url)); } %> updateProc.jsp
else if (action.equals("delete")) { int seq = Integer.parseInt(request.getParameter("seq")); flag = dao.delete(seq); if(flag == true) { Vector v1 = dao.list(); request.setAttribute("v1", v1); url = "/Demo8/listMember.jsp"; } else { dto = dao.read(seq); url = "/Demo8/readMember.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(url); dispatcher.forward(request, response); } 36
<% boolean flag = dao.delete(dto.getSeq()); if(flag == true) { String url = "listMember.jsp"; response.sendRedirect(url); } else { String url = "readMember.jsp?seq="+dto.getSeq(); response.sendRedirect(response.encodeRedirectURL(url)); } %> deleteProc.jsp
#. Settings web.xml WebContent\WEB-INF\web.xml MVC2 MemberServlet mvc2.member.controller.MemberServlet MemberServlet /action.do 38
View JSP Source – 입력 폼 : inputMember.jsp – 회원 목록 : listMember.jsp – 회원 상세 : readMember.jsp – 회윈 등록 처리부 : createProc.jsp – 회원 수정 처리부 : updateProc.jsp – 회원 삭제 처리부 : deleteProc.jsp Servlet
MVC2 Member function validCheck(f){ return true; } function goList(){ location.href="/LKH_PRJ/action.do?actionType=list"; } Demo8 : MVC2 member > 회원 입력폼 inputMember.jsp
이름 아이디 비밀번호
<% Vector vt = new Vector(); vt = dao.list(); %> MVC2 Member Demo8 : MVC2 member > 회원 목록 번호 이름 아이디 비밀번호 등록일 listMember.jsp
등록된 회원이 없습니다 <% } else { int viewNum = 1; for(int i=0; i<vt.size(); i++) { dto = (MemberVO)vt.get(i); %> "> <% viewNum++; } %>
회원정보 입력
<% request.setCharacterEncoding("euc-kr"); MemberDTO dto = (MemberDTO)request.getAttribute("dto"); %> MVC2 Member function goAction(actionPrm){ document.memForm.action="/LKH_PRJ/action.do"; document.memForm.actionType.value = actionPrm; document.memForm.submit(); } readMember.jsp
function goList(){ location.href="./listMember.jsp"; } Demo8 : MVC2 member > 회원정보 상세보기 "> 번호 아이디 ">
이름 "> 비밀번호 "> 등록일
Result [ 리스트 ] [ 등록 ] [ 보기, 수정, 삭제 ]