480 likes | 716 Views
Ch13. 웹 프로그래밍 설계 및 실습 MVC2 Member. 이경화 (khlee@cherry.ssu.ac.kr). First off. http://java.sun.com/ http://java.sun.com/j2se/1.4.2/docs/api/index.html http://java.sun.com/javase/6/docs/api/ [ 영문 API_1.6] http://apollo89.com/java/ko/api/ [ 한글 API_1.6]
E N D
Ch13.웹 프로그래밍 설계 및 실습MVC2 Member 이경화(khlee@cherry.ssu.ac.kr)
First off http://java.sun.com/ http://java.sun.com/j2se/1.4.2/docs/api/index.html http://java.sun.com/javase/6/docs/api/ [영문API_1.6] http://apollo89.com/java/ko/api/ [한글API_1.6] http://www.w3schools.com/TAGS/ [HTML 4.01 / XHTML 1.0 Reference] http://java.sun.com/docs/books/tutorial/javabeans/ [The JavaBeans™ Tutorial] Servlet and JSP, C.Edward Chow
Contents • Basic MVC2 Pattern • JSP Model 2 architecture - Member
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 동작 과정 VIEW listMember.jsp list createMember.jsp createProc.jsp create DBConnection.java readMember.jsp DAO.java read DTO.java update MODEL DB updateProc.jsp deleteProc.jsp delete 7
MVC2 member 동작 과정 VIEW main.jsp list listMember.jsp CONTROLLER MemberServlet.java createMember.jsp create DBConnection.java DAO.java readMember.jsp read DTO.java MODEL DB update / delete 8
Model • - DB연결 : mvc2.db.DBConn.java • - DAO : mvc2.member.dao.MemberDAO.java • - DTO : mvc2.member.dto.MemberVO.java 9
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 연결 • * @return Connection • */ • public static Connection mysqlConn() { • Connection conn = null; • String url= "jdbc:mysql://127.0.0.1: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 연결 해제 • * @param 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 연결 해제 • * @param conn • * @param 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 연결 해제 • * @param conn • * @param pstmt • * @param 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
/** • * 회원 정보 생성 • * @param dto • * @return 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; • }
/** • * 회원 목록 보기 • * @return 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; • }
/** • * 회원정보 상세보기 • * @param idVal • * @return 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; • }
/** • * 회원 정보 수정 • * @param dto • * @return 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; • }
/** • * 회원정보 삭제 • * @param dto • * @return 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 mvc2.member.controller MemberServlet 없는 경우 : Controller선택>마우스우클릭>other>Web>servlet 선택 27
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; /** * @see HttpServlet#HttpServlet() */ public MemberServlet() { super(); } MVC2 방식 MemberServlet.java 자동생성 28
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.myCall(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.myCall(request, response); } } 29
public voidmyCall(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
<%@page contentType="text/html; charset=euc-kr" pageEncoding="euc-kr"%> <% request.setCharacterEncoding("euc-kr"); %> <jsp:useBean id="dao" class="mvc1.member.dao.MemberDAO"/> <jsp:useBean id="dto" class="mvc1.member.dto.MemberVO"/> <jsp:setProperty name="dto" property="*"/> <% 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
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> • <% request.setCharacterEncoding("euc-kr"); %> • <jsp:useBean id="dao" class="mvc1.member.dao.MemberDAO"/> • <jsp:useBean id="dto" class="mvc1.member.dto.MemberVO"/> • <jsp:setProperty name="dto" property="*"/> • <% • 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
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> • <jsp:useBean id="dao" class="mvc1.member.dao.MemberDAO"/> • <jsp:useBean id="dto" class="mvc1.member.dto.MemberVO"/> • <jsp:setProperty name="dto" property="*"/> • <% • 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 • <servlet> • <display-name>MVC2</display-name> • <servlet-name>MemberServlet</servlet-name> • <servlet-class>mvc2.member.controller.MemberServlet</servlet-class> • </servlet> • <servlet-mapping> • <servlet-name>MemberServlet</servlet-name> • <url-pattern>/action.do</url-pattern> • </servlet-mapping> WebContent\WEB-INF\web.xml 38
View • JSP Source • 입력 폼 : inputMember.jsp • 회원 목록 : listMember.jsp • 회원 상세 : readMember.jsp • 회윈 등록 처리부 : createProc.jsp • 회원 수정 처리부 : updateProc.jsp • 회원 삭제 처리부 : deleteProc.jsp Servlet
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <html> <head> <title> MVC2 Member</title> <script language="javascript"> function validCheck(f){ return true; } function goList(){ location.href="/LKH_PRJ/action.do?actionType=list"; } </script> </head> <body><h3>Demo8 : MVC2 member > 회원 입력폼</h3><hr> <table border="1" width="400"> <form method="post" action="/LKH_PRJ/action.do" onSubmit="return validCheck(this)"> <input type=hidden name="actionType" value="create"> inputMember.jsp
<tr> <td>이름</td> <td><input type="text" name="username"></td></tr> <tr> <td>아이디 </td> <td><input type="text" name="userid"></td></tr> <tr> <td>비밀번호 </td> <td><input type="text" name="userpw"></td></tr> <tr> <td colspan="2" align="center"> <input type="submit" value=" 저장 "> <input type="button" value=" 목록 " onClick="javascript:goList()"> </td> </tr> </form> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR” %> • <%@page import="java.util.*, mvc2.member.dto.*"%> • <% request.setCharacterEncoding("euc-kr"); %> • <jsp:useBean id="dao" class="mvc2.member.dao.MemberDAO"/> • <jsp:useBean id="dto" class="mvc2.member.dto.MemberVO"/> • <% • Vector vt = new Vector(); • vt = dao.list(); • %> • <html> • <head><title> MVC2 Member</title></head> • <body> • <h3>Demo8 : MVC2 member > 회원 목록</h3><hr> • <TABLE width="550" border="1"> • <tr> • <td>번호</td> • <td>이름</td> • <td>아이디</td> • <td>비밀번호</td> • <td>등록일</td> • </tr> listMember.jsp
<% if(vt.isEmpty()) { %> • <tr><td colspan='5'>등록된 회원이 없습니다</center></td></tr> • <% } else { • int viewNum = 1; • for(int i=0; i<vt.size(); i++) { • dto = (MemberVO)vt.get(i); • %> • <tr> • <td> • <a href="/LKH_PRJ/action.do?actionType=read&seq=<%=dto.getSeq()%>"> • <%=viewNum%></a> </td> • <td><%=dto.getUserid()%></td> • <td><%=dto.getUsername()%></td> • <td><%=dto.getUserpw()%></td> • <td><%=dto.getRegdate()%></td> • </tr> • <% • viewNum++; • } • } • %>
</TABLE> <a href="./inputMember.jsp">회원정보 입력</a> </BODY> </HTML>
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR" import="mvc2.member.dto.*"%> • <% request.setCharacterEncoding("euc-kr"); %> • <jsp:useBean id="dao" class="mvc2.member.dao.MemberDAO"/> • <jsp:useBean id="dto" class="mvc2.member.dto.MemberVO"/> • <% • request.setCharacterEncoding("euc-kr"); • MemberDTO dto = (MemberDTO)request.getAttribute("dto"); • %> • <html> • <head> • <title> MVC2 Member</title> • <script language="javascript"> • 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"; } </script> </head> <BODY> <h3>Demo8 : MVC2 member > 회원정보 상세보기</h3><hr> <TABLE width="550" border="1"> <form name="memForm" method="post"> <input type=hidden name=seq value="<%=dto.getSeq()%>"> <input type=hidden name="actionType"> <tr><td>번호 </td><td><%=dto.getSeq()%></td></tr> <tr> <td>아이디 </td> <td> <input type="text" name="userid" value="<%=dto.getUserid()%>"> </td> </tr>
<tr> <td>이름 </td> <td> <input type="text“ name="username“ value="<%=dto.getUsername()%>"> </td> </tr> <tr> <td>비밀번호</td> <td> <input type="text" name="userpw“ value="<%=dto.getUserpw()%>"> </td> </tr> <tr><td>등록일 </td><td><%=dto.getRegdate()%></td></tr> <tr> <td colspan="2" align="center"> <input type="button" value=" 수정" onClick="javascript:goAction('update')"> <input type="button" value=" 삭제" onClick="javascript:goAction('delete')"> <input type="button" value=" 목록 " onClick="javascript:goAction('iist')"> </td> </tr> </form> </TABLE> </BODY> </HTML>
Result [등록] [리스트] http://127.0.0.1/LKH_PRJ/Demo8/listMember.jsp [보기, 수정, 삭제]