728x90
6단계 JSP만 include, redirect처리
1. include 처리
1) 회원가입 JSP파일을 회원정보리스트 페이지에 include처리
m_list.jsp
1 2 | <%@ include file="/minsert/m_insert_form.jsp"%> <!-- *** directive include --> | cs |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager"%> <%@ page import = "java.sql.Connection"%> <%@ page import = "java.sql.PreparedStatement"%> <%@ page import = "java.sql.ResultSet"%> <%@ page import = "java.sql.SQLException"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원 정보 리스트</title> </head> <body> <%@ include file="/minsert/m_insert_form.jsp"%> <!-- *** directive include --> <%= request.getRequestURI()%> <br> 회원 리스트 <br> <table width="100%" border="1"> <tr> <td>아이디</td> <td>비밀번호</td> <td>권한</td> <td>이름</td> <td>이메일</td> <td>수정</td><!-- **수정버튼작성 --> <td>삭제</td><!-- ***삭제버튼생성 --> </tr> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // ResultSet클래스형 데이터 타입으로 객체참조변수 rs를 선언하고 null로 초기화 //JDBC 프로그램 순서 //01단계 :드라이버 로딩 시작 Class.forName("com.mysql.jdbc.Driver"); //01단계 :드라이버 로딩 끝 try{ //02단계 :DB연결(Connection)시작 String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); //02단계 :DB연결(Connection)끝 System.out.println(conn + "<-- conn m_list.jsp"); if(conn != null){ out.println("01 DB연결 성공"); }else{ out.println("02 DB연결 실패"); } //03단계 :Query실행을 위한 statement 또는 prepareStatement객체생성 시작 pstmt = conn.prepareStatement("select * from tb_member"); //04단계 :Query실행 시작 rs = pstmt.executeQuery(); System.out.println(rs + "<-- rs m_list.jsp"); //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //04단계 :Query실행 끝 //05단계 :Query실행결과 사용 //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //select문장 통해서 모든 회원 목록 가져와서 한줄씩 //(레코드(record) or 로우(row))보여준다 시작 while(rs.next()){ %> <tr> <td><%= rs.getString("m_id")%></td> <td><%= rs.getString("m_pw")%></td> <td><%= rs.getString("m_level")%></td> <td><%= rs.getString("m_name")%></td> <td><%= rs.getString("m_email")%></td> <td> <a href="<%= request.getContextPath() %> /mupdate/m_update_form.jsp?send_id=<%= rs.getString("m_id")%>">수정클릭</a> <!-- **수정버튼 클릭시 해당id값을 가지고 수정화면으로 이동 --> </td> <td> <a href="<%= request.getContextPath() %> /mdelete/m_delete_pro.jsp?send_id=<%= rs.getString("m_id")%>">삭제클릭</a> <!-- **삭제버튼 클릭시 해당id값의 모든정보를 삭제--> </td> </tr> <% } //select문장 통해서 모든 회원 목록 가져와서 한줄씩 //(레코드(record) or 로우(row))보여준다 끝 } catch(SQLException ex) { out.println(ex.getMessage()); ex.printStackTrace(); } finally { // 6. 사용한 Statement 종료 if (rs != null) try { rs.close(); } catch(SQLException ex) {} if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} // 7. 커넥션 종료 if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </table> </body> </html> | cs |
include 후의 모습
2) 검색 화면 JSP파일을 회원정보 검색 리스트 페이지에 include처리
m_search_list.jsp
1 2 | <%@ include file="/msearch/m_search_form.jsp"%> <!-- ***directive include --> | cs |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager"%> <%@ page import = "java.sql.Connection"%> <%@ page import = "java.sql.PreparedStatement"%> <%@ page import = "java.sql.ResultSet"%> <%@ page import = "java.sql.SQLException"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원정보 검색 리스트</title> </head> <body> <%@ include file="/msearch/m_search_form.jsp"%> <!-- ***directive include --> <h4>회원 리스트</h4> <table width="100%" border="1"> <tr> <td>아이디</td> <td>비밀번호</td> <td>권한</td> <td>이름</td> <td>이메일</td> <td>수정</td> <td>삭제</td> </tr> <% request.setCharacterEncoding("utf-8"); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; //JDBC 프로그램 순서 //01단계 :드라이버 로딩 시작 Class.forName("com.mysql.jdbc.Driver"); String sk = request.getParameter("sk"); String sv = request.getParameter("sv"); System.out.println(sk + "<-- sk m_search_list.jsp"); System.out.println(sv + "<-- sv m_search_list.jsp"); try{ String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); if(sk == null & sv == null){ out.println("01 sk null, sv null인 조건"); pstmt = conn.prepareStatement("select * from tb_member"); //select * from tb_member; }else if(sk != null & sv.equals("")){ out.println("02 sk 값O, sv 공백 조건"); pstmt = conn.prepareStatement("select * from tb_member"); //select * from tb_member; }else if(sk != null & sv != null){ out.println("03 sk, sv 둘다 값 O"); pstmt = conn.prepareStatement("select * from tb_member where "+sk+"=?"); pstmt.setString(1, sv); //select * from tb_member where m_id='id001'; //select * from tb_member where m_level='관리자'; //select * from tb_member where m_name='홍02'; //select * from tb_member where m_email='email03'; } out.println("<br>"+pstmt + "<--- pstmt"); rs = pstmt.executeQuery(); System.out.println(rs + "<-- rs m_list.jsp"); while(rs.next()){ %> <tr> <td><%= rs.getString("m_id")%></td> <td><%= rs.getString("m_pw")%></td> <td><%= rs.getString("m_level")%></td> <td><%= rs.getString("m_name")%></td> <td><%= rs.getString("m_email")%></td> <td> <a href="<%= request.getContextPath() %> /mupdate/m_update_form.jsp?send_id=<%= rs.getString("m_id")%>">수정클릭</a> </td> <td> <a href="<%= request.getContextPath() %> /mdelete/m_delete_pro.jsp?send_id=<%= rs.getString("m_id")%>">삭제클릭</a> </td> </tr> <% } }catch(SQLException ex) { out.println(ex.getMessage()); ex.printStackTrace(); } finally { // 6. 사용한 Statement 종료 if (rs != null) try { rs.close(); } catch(SQLException ex) {} if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} // 7. 커넥션 종료 if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </table> </body> </html> | cs |
include 후의 모습
2. redirect처리
1) 입력 처리 완료 후에 m_list.jsp로 redirect
m_insert_pro.jsp
1 | response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); | cs |
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <%@ page import = "java.sql.DriverManager"%> <%@ page import = "java.sql.Connection"%> <%@ page import = "java.sql.PreparedStatement"%> <%@ page import = "java.sql.SQLException"%> <!-- 특정패키지들 내에 클래스들 내에 선언된 메서드들의 기능들을 호출하기위해서 import --> <% request.setCharacterEncoding("utf-8"); // 입력 받은 한글이 깨지지 않도록 UTF-8세팅. *getParameter위에 써야함 Connection conn = null; // Connection클래스 데이터타입으로 객체참조 변수 conn 선언 후 null로 초기화 PreparedStatement pstmt = null; // PreparedStatement클래스 데이터타입으로 객체참조 변수 pstmt 선언 후 null로 초기화 //화면에서 입력데이터를 받아서 각각 변수에 저장하고 콘솔창에 출력 시작 String m_id = request.getParameter("m_id"); String m_pw = request.getParameter("m_pw"); String m_level = request.getParameter("m_level"); String m_name = request.getParameter("m_name"); String m_email = request.getParameter("m_email"); System.out.println(m_id + "<-- m_id /minsert/m_insert_pro.jsp"); System.out.println(m_pw + "<-- m_pw /minsert/m_insert_pro.jsp"); System.out.println(m_level + "<-- m_level /minsert/m_insert_pro.jsp"); System.out.println(m_name + "<-- m_name /minsert/m_insert_pro.jsp"); System.out.println(m_email + "<-- m_email /minsert/m_insert_pro.jsp"); //화면에서 입력데이터를 받아서 각각 변수에 저장하고 콘솔창에 출력 시작 // * JDBC 프로그램 순서 //01단계 :드라이버 로딩 시작 Class.forName("com.mysql.jdbc.Driver"); //01단계 :드라이버 로딩 끝 try{ //02단계 :DB연결(Connection)시작 String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?"+ "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); System.out.println(conn + "<-- conn"); System.out.println(conn.getClass() + "<-- conn.getClass()"); //02단계 :DB연결(Connection)끝 //03단계 :Query실행을 위한 statement 또는 prepareStatement객체생성 시작 pstmt = conn.prepareStatement( "INSERT INTO tb_member VALUES (?, ?, ?, ?, ?)"); System.out.println(pstmt + "<-- pstmt 1"); System.out.println(pstmt.getClass() + "<-- pstmt.getClass() 1"); //insert into tb_member values('id001','pw001','관리자','홍01','email01'); pstmt.setString(1, m_id); pstmt.setString(2, m_pw); pstmt.setString(3, m_level); pstmt.setString(4, m_name); pstmt.setString(5, m_email); System.out.println(pstmt + "<-- pstmt 2"); //03단계 :Query실행을 위한 statement 또는 prepareStatement객체생성 끝 //04단계 :Query실행 시작 pstmt.executeUpdate(); //04단계 :Query실행 끝 //05단계 :Query실행결과 사용 (insert의 경우 생략 가능단계) response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); // 회원가입 후 리스트페이지로 이동 // sendRedirect -> 리스트페이지로 리다이겍트처리 }finally{ //06단계 :statement 또는 prepareStatement객체 종료(close()) if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} //07단계 :Connection 객체 종료(close()) if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> | cs |
2) 수정 처리 완료 후에 m_list.jsp로 redirect
m_update_pro.jsp
1 | response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); | cs |
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager"%> <%@ page import = "java.sql.Connection"%> <%@ page import = "java.sql.PreparedStatement"%> <%@ page import = "java.sql.SQLException"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원정보 수정 처리</title> </head> <body> <% request.setCharacterEncoding("utf-8"); Connection conn = null; PreparedStatement pstmt = null; //화면에서 입력자료 받아서 콘솔창에 출력 시작 String m_id = request.getParameter("m_id"); String m_pw = request.getParameter("m_pw"); String m_level = request.getParameter("m_level"); String m_name = request.getParameter("m_name"); String m_email = request.getParameter("m_email"); System.out.println(m_id + "<-- m_id /mupdate/m_update_pro.jsp"); System.out.println(m_pw + "<-- m_pw /mupdate/m_update_pro.jsp"); System.out.println(m_level + "<-- m_level /mupdate/m_update_pro.jsp"); System.out.println(m_name + "<-- m_name /mupdate/m_update_pro.jsp"); System.out.println(m_email + "<-- m_email /mupdate/m_update_pro.jsp"); Class.forName("com.mysql.jdbc.Driver"); //01단계 :드라이버 로딩 끝 try{ //02단계 :DB연결(Connection)시작 String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); pstmt = conn.prepareStatement( "UPDATE tb_member SET m_pw=?,m_level=?,m_name=?,m_email=? WHERE m_id=?"); System.out.println(pstmt + "<-- pstmt 1"); System.out.println(pstmt.getClass() + "<-- pstmt.getClass() 1"); pstmt.setString(1, m_pw); pstmt.setString(2, m_level); pstmt.setString(3, m_name); pstmt.setString(4, m_email); pstmt.setString(5, m_id); System.out.println(pstmt + "<-- pstmt 2"); pstmt.executeUpdate(); response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); // 회원정보 수정처리 후 회원정보 리스트로 redirect }finally{ if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </body> </html> | cs |
3) 삭제 처리 완료 후에 m_list.jsp로 redirect
m_delete_pro.jsp
1 | response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); | cs |
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"%> <%@ page import = "java.sql.DriverManager"%> <%@ page import = "java.sql.Connection"%> <%@ page import = "java.sql.PreparedStatement"%> <%@ page import = "java.sql.SQLException"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원정보 삭제 처리</title> </head> <body> <% String send_id = request.getParameter("send_id"); System.out.println(send_id + "<-- send_id m_delete_pro.jsp"); request.setCharacterEncoding("utf-8"); Connection conn = null; PreparedStatement pstmt = null; Class.forName("com.mysql.jdbc.Driver"); try{ String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); pstmt = conn.prepareStatement( "DELETE FROM tb_member WHERE m_id=?"); pstmt.setString(1, send_id); pstmt.executeUpdate(); response.sendRedirect(request.getContextPath()+"/mlist/m_list.jsp"); // 회원정보 삭제 처리 후 회원정보리스트로 redirect }finally{ if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </body> </html> | cs |