JSP/day37-38 / 23.10.24-25(화,수) / (핀테크) Spring 및 Ai 기반 핀테크 프로젝트 구축

허니몬·2023년 10월 25일
0
post-thumbnail

07_Student


webapp/layout/*.jsp

layout


header.jsp





<!-- layout/header.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
<style type="text/css">
#main {
	width: 500px;
}
caption {
	font-size: 30px;
	font-weight: bold;
	border: 1px solid #999999;
	margin-bottom: 10px;
	padding: 10px 0;
}
.btn {
	font-weight: bold;
}
</style>
</head>
<body>
	<div align="center">
		<table id="main">
			<caption>학사 관리</caption>
			<tr>
				<th>
					<input type="button" class="btn" value="입 력" onclick="document.location.href='/07_Student/insert/codeInput.jsp'"/>
				</th>
				<th>
					<input type="button" class="btn" value="수 정" onclick="document.location.href='/07_Student/update/updateForm.jsp'"/>
				</th>
				<th>
					<input type="button" class="btn" value="삭 제" onclick="document.location.href='/07_Student/delete/deleteForm.jsp'"/>
				</th>
				<th>
					<input type="button" class="btn" value="목 록" onclick="document.location.href='/07_Student/search/select.jsp'"/>
				</th>
			</tr>
			<tr>
				<td colspan="4" align="center">
					<input type="button" class="btn" value="처 음" onclick="document.location.href='/07_Student/index.jsp'"/>
				</td>
			</tr>
		</table>
	</div>

footer.jsp

<!-- layout/footer.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

</body>
</html>

index.jsp

webapp/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/layout/header.jsp"%> 

<%@ include file="/layout/footer.jsp"%> 


java/school.../...

schooldto


SchoolDTO.java

package school.dto;

/*
CREATE TABLE school(
NAME VARCHAR2(15) NOT NULL, -- 이름
VALUE VARCHAR2(15),         -- 학생 : 학번, 교수 : 과목명, 관리자 : 부서명
CODE NUMBER                 -- 관리 코드 : 1.학생 2.  교수 3.  관리자
);
 */
public class SchoolDTO {
	private String name;
	private String value;
	private int code;
	
	public SchoolDTO() {}
	
	public SchoolDTO(String name, String value, int code) {
		this.name = name;
		this.value = value;
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}
	
	
}

schooldao


SchoolDAO.java

package school.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import school.dto.SchoolDTO;


public class SchoolDAO {
	private String url = "jdbc:oracle:thin:@localhost:1521:xe";
	private String id = "dbtest";
	private String pwd = "a1234";
	
	public SchoolDAO() {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			System.out.println("로딩 성공");
		} catch (Exception e) {
			System.out.println("로딩 실패 ㅠㅠ");
			e.printStackTrace();
		}
	}// SchoolDAO() end
	
	public Connection getConnection() {
		Connection con = null;
		try {
			// DB 연결 객체 생성
			con = DriverManager.getConnection(url,id,pwd);
			System.out.println("연결 성공!!");
		} catch (Exception e) {
			System.out.println("연결 실패~");
			e.printStackTrace();
		}
		return con;
	}// getConnection() end
	
	// 추가
	public boolean insert(SchoolDTO dto) {
		Connection con = null;
		PreparedStatement pstmt = null;
		boolean check = false;
		try {
			String sql = "INSERT INTO school VALUES(?,?,?)";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1,dto.getName());
			pstmt.setString(2,dto.getValue());
			pstmt.setInt(3,dto.getCode());
			int su = pstmt.executeUpdate(); // 추가 성공한 레코드 수 반환
			if(su > 0) check = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) {
					pstmt.close();
				}
				if(con != null) {
					con.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(dto.getName()+"정보 추가");
		return check;
		
	}// insert() end
	
	// 목록
	public ArrayList<SchoolDTO> getList() {
		ArrayList<SchoolDTO> list = new ArrayList<>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		
		try {
			String sql = "SELECT * FROM SCHOOL";
			con = this.getConnection();
			pstmt = con.prepareStatement(sql);
			res = pstmt.executeQuery();
			
			while(res.next()) {
				String name = res.getString("name");
				String value = res.getString("value");
				int code = res.getInt("code");
				
				list.add(new SchoolDTO(name, value, code));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) {
					pstmt.close();
				}
				if(con != null) {
					con.close();
				}
				if(res != null) {
					res.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(list.isEmpty()){
			list = null;
		}
		return list;
		
	}// getList() end
	
	
	
	// 검색
	public void search(String sname) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		try {
			String sql = "SELECT * FROM SCHOOL WHERE NAME = ?";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, sname);
			res = pstmt.executeQuery();
			if(res == null) {
				System.out.println(sname + "임마 없음");
			} else {
				while(res.next()) {
					String name = res.getString("name");
					String value = res.getString("value");
					int code = res.getInt("code");
					System.out.println(name +" - "+ value +" - "+ code); 
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res != null) res.close();
				if(pstmt != null) pstmt.close();
				if(con != null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}// search() end
	
	
	// 검색2 
	public void search2(SchoolDTO dto) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		try {
			String sql = "SELECT * FROM SCHOOL WHERE ";
			con = getConnection();
			if(dto.getName()!=null) {
				pstmt = con.prepareStatement(sql + "NAME =?");
				pstmt.setString(1, dto.getName());
			} else if(dto.getCode()!=0) {
				pstmt = con.prepareStatement(sql + "CODE =?");
				pstmt.setInt(1, dto.getCode());
			}
			res = pstmt.executeQuery();
			if(res==null) {
				System.out.println("없음");
			} else {
				while(res.next()) {
					String namea = res.getString("name"); 
					String value = res.getString("value"); 
					int code = res.getInt("code"); 
					System.out.println(namea + " - " + value + " - " + code);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!=null)con.close();
				if(pstmt!=null)pstmt.close();
				if(res!=null)res.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	// 검색T
	public void searchT(SchoolDTO dto) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		try {
			String sql = null;
			if(dto == null) {
				sql = "select * from school";
			} else if(dto.getName()!=null) {
				sql = "select * from school where name like ?";
			} else {
				sql = "select * from school where code=?";
			}
			
			con =  this.getConnection();
			pstmt = con.prepareStatement(sql);
			if(dto !=null) {
				if(dto.getName()!=null) {
					pstmt.setString(1, "%" + dto.getName() + "%");
				}else {
					pstmt.setInt(1, dto.getCode());
				}
			}
			res = pstmt.executeQuery();
			while(res.next()) {
				String name = res.getString("name");
				String value = res.getString("value");
				int code= res.getInt("code");
				System.out.print(name + "\t");
				if(code == 1) {
					System.out.println("학번 : " + value);
				} else if(code ==2 ) {
					System.out.println("과목 : " + value);
				} else{
					System.out.println("부서 : " + value);
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!= null)con.close();
				if(res!= null)res.close();
				if(pstmt!= null)pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	// 삭제
	public int delete(String dname) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet resS =null;
		int res = 0;
		try {
			String sqlsel = "SELECT * FROM MEMBER";
			con = getConnection();
			pstmt = con.prepareStatement(sqlsel);
			resS = pstmt.executeQuery();
			if(resS == null) {
				System.out.println("조회된 데이터가 없습니다.");
			} else {
				String sql = "DELETE FROM SCHOOL WHERE NAME = ?";
				con = getConnection();
				pstmt = con.prepareStatement(sql);
				pstmt.setString(1, dname);
				res = pstmt.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(res + "개 행 삭제");
		return res;
	}
	// 수정
	public int update(SchoolDTO dto, String aname) {
		Connection con = null;
		PreparedStatement pstmt = null;
		int res =0;
		try {
			String sql = "UPDATE SCHOOL SET NAME=?,VALUE=?, CODE=? WHERE NAME=?";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, aname);
			pstmt.setString(2, dto.getValue());
			pstmt.setInt(3, dto.getCode());
			pstmt.setString(4, dto.getName());
			res = pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstmt !=null) pstmt.close();
				if(con !=null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(res + " 개 정보 수정");
		return res;
	}
}

insert


codeInput.jsp

<!-- insert/codeInput.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/layout/header.jsp"%>
	<div align="center">
		<h2>코드 선택</h2>
		<table width="300px">
			<tr>
				<th>
					<button onclick="document.location.href='/07_Student/insert/insertForm.jsp?code=1'">학 생</button>
				</th>
				<th>
					<button onclick="document.location.href='/07_Student/insert/insertForm.jsp?code=2'">교 수</button>
				</th>
				<th>
					<button onclick="document.location.href='/07_Student/insert/insertForm.jsp?code=3'">직 원</button>
				</th>
			</tr>
		</table>
	</div>
<%@ include file="/layout/footer.jsp"%>

insertForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
int code = Integer.parseInt(request.getParameter("code"));

String title = null;
switch(code){
case 1:
	title = "학 번";
	break;
case 2:
	title = "과 목";
	break;
case 3:
	title = "부 서";
	break;

}
/* out.println(code + " : " + title); */
%>
<%@ include file="/layout/header.jsp"%> 
	<div align="center">
		<h2>정보 입력</h2>
		<form name="input" action="insert.jsp" method="post">
			<input type="hidden" name="code" value="<%=code %>"/>
			<table border="1" width="300px">
				<tr>
					<th width="100px">이 름</th><td><input type="text" name="name"/></td> 
				</tr>
				<tr>
					<th width="100px"><%=title %></th><td><input type="text" name="value"/></td> 
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="button" value="완료" onclick="check()"/> &nbsp;
						<input type="reset" value="취소"/>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<!-- script -->
	<script type="text/javascript">
		function check() {
			if(document.input.name.value==""){
				alert("이름을 입력하세요~");
				document.input.name.focus();
			}else if(document.input.value.value==""){
				alert("<%=title %>을 입력하세요~");
				document.input.value.focus();
			} else {
				document.input.submit();
			}
		}
	</script>
<%@ include file="/layout/footer.jsp" %> 

insert.jsp

<%@page import="school.dao.SchoolDAO"%>
<%@page import="school.dto.SchoolDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String value = request.getParameter("value");
int code = Integer.parseInt(request.getParameter("code"));

SchoolDTO dto = new SchoolDTO(name, value, code);
SchoolDAO dao = new SchoolDAO();

String msg = null;
boolean check = dao.insert(dto);
if(check) msg = name + "님이 등록 되었습니다..";
else msg = "등록 실패";

request.setAttribute("msg", msg);

pageContext.forward("/insert/result.jsp");

%>

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String msg = (String)request.getAttribute("msg");
%>
<%@ include file="/layout/header.jsp"%>
	<div align="center">
		<h2>등록 결과</h2>
		<h3><%=msg %></h3>
	</div>
<%@ include file="/layout/footer.jsp"%>


select.jsp

<%@page import="school.dto.SchoolDTO"%>
<%@page import="school.dao.SchoolDAO"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
SchoolDAO dao = new SchoolDAO();
List<SchoolDTO> list = dao.getList();
String codeStn = null;


%>
<%@ include file="/layout/header.jsp"%>
	<br>
	<div align="center">
	<%if(list == null){%>
		<h2>데이터가 없습니다~</h2>
	<% } else {%>
		<table border="1">
			<tr>
				<th width="120px"> NAME </th>			
				<th width="140px"> VALUE </th>			
				<th width="120px"> CODE </th>			
			</tr>
			<%for(SchoolDTO dto : list){%>
				<tr>
					<td><%=dto.getName() %></td>
					<td><%=dto.getValue() %></td>
					<td>
						<%
						switch(dto.getCode()){
						case 1:
							codeStn ="학생";
							break;
						case 2:
							codeStn ="교수";
							break;
						case 3:
							codeStn ="직원";
							break;
						}
						%>
						<%=codeStn %>
					</td>
				</tr>
			<%} %>
		</table>
	<%} %>		
	</div>
<%@ include file="/layout/footer.jsp"%>

update


updateForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/layout/header.jsp"%>
	<div align="center">
		<h2>정보 수정</h2>
		<form name="update" action="update.jsp" method="post">
			<table border="1" width="300px">
				<tr>
					<th width="120px">수정 전 이름</th><td><input type="text" name="bname"/></td>
				</tr>			
				<tr>
					<th width="120px">수정 후 이름</th><td><input type="text" name="aname"/></td>
				</tr>			
				<tr>
					<th width="120px">수정 값</th><td><input type="text" name="value"/></td>
				</tr>			
				<tr>
					<th>수정 코드</th>
					<td>
						<select name="code">
							<option value="1">학생</option>
							<option value="2">교수</option>
							<option value="3">직원</option>
						</select>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="button" value="완료" onclick="check()"/> &nbsp;
						<input type="reset" value="취소"/>
					</td>
				</tr>
				
			</table>
		</form>
	</div>
	<!-- script -->
	<script type="text/javascript">
		function check() {
			if(document.update.bname.value==""){
				alert("수정 전 이름을 입력하세요~");
				document.update.bname.focus();
			}else if(document.update.aname.value==""){
				alert("수정 후 이름을 입력하세요~");
				document.update.aname.focus();
			}else if(document.update.value.value==""){
				alert("수정 값을 입력하세요~");
				document.update.value.focus();
			} else {
				document.update.submit();
			}
		}
	</script>
<%@ include file="/layout/footer.jsp"%>

update.jsp

<%@page import="school.dto.SchoolDTO"%>
<%@page import="school.dao.SchoolDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
request.setCharacterEncoding("utf-8");
String bname = request.getParameter("bname");
String aname = request.getParameter("aname");
String value = request.getParameter("value");
int code = Integer.parseInt(request.getParameter("code"));

SchoolDTO dto = new SchoolDTO(bname, value, code);
SchoolDAO dao = new SchoolDAO();

String msg = null;
int check = dao.update(dto, aname);
if(check>0) msg = check +"개 정보 수정";
else msg = "수정 X";

request.setAttribute("msg", msg);
pageContext.forward("/update/result.jsp");
%>

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String msg = (String)request.getAttribute("msg");
%>
<%@ include file="/layout/header.jsp"%>
	<div align="center">
		<h2>등록 결과</h2>
		<h3><%=msg %></h3>
	</div>
<%@ include file="/layout/footer.jsp"%>

delete


deleteForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/layout/header.jsp"%>
	<br/>
	<div align="center">
		<h2>삭제 정보 입력</h2>
		<form name="delete" action="delete.jsp" method="post">
		<table border="1" width="300px">
			<tr>
				<th width="100px">삭제 이름</th><td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="button" value="삭제" onclick="check()" /> &nbsp; 
					<input type="reset" value="취소" />
				</td>
			</tr>
		</table>
		</form>
	</div>
	<!-- script -->
	<script type="text/javascript">
		function check() {
			if(document.delete.name.value==""){
				alert("삭제 이름 입력");
				document.delete.name.focus();
			} else {
				document.delete.submit();
			}
		}
	</script>
<%@ include file="/layout/footer.jsp"%>

delete.jsp

<%@page import="school.dao.SchoolDAO"%>
<%@page import="school.dto.SchoolDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");

SchoolDTO dto = new SchoolDTO();
SchoolDAO dao = new SchoolDAO();

String msg = null;
int res = dao.delete(name);
if(res==0){
	msg = name + " 얘 없어";
} else {
	msg = name + " 정보 삭제";
}
request.setAttribute("msg", msg);
request.setAttribute("res", res);
pageContext.forward("/delete/result.jsp");
%>    

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String msg = (String)request.getAttribute("msg");
int res = (Integer)request.getAttribute("res");
%>    
<%@ include file="/layout/header.jsp"%>
	<div align="center">
		<h2>삭제 결과</h2>
		<h3><%=msg %></h3>
		<h3><%=res %> 개 삭제</h3>
	</div>
<%@ include file="/layout/footer.jsp"%>    

08_member


MVC.txt

MVC 패턴

  • MVC 는 모델(Model), 뷰(View), 컨트롤러(Controller) 의 약자로, 소프트웨어를 개발하는 방법론
  • 모델 : 업무 처리 로직(비즈니스 로직) 혹은 데이터베이스와 관련된 작업을 담당
  • 뷰 : JSP 페이지와 같이 사용자에게 보여지는 부분을 담당
  • 컨트롤러 : 모델과 뷰를 제어하는 역할
    사용자의 요청을 받아서 그 요청을 분석하고, 필요한 업무 처리 로직(모델)을 호출
    모델이 결과값을 반환하면 출력할 뷰(JSP 페이지)를 선택 후 전달

member_table.txt

CREATE TABLE member(
name VARCHAR2(30) NOT NULL, -- 이름
id VARCHAR2(30) PRIMARY KEY, -- id
pwd VARCHAR2(30) NOT NULL, -- 비밀번호
gender VARCHAR2(3), -- 성별
email VARCHAR2(20), -- 이메일
domain VARCHAR2(20), -- domain
tel VARCHAR2(13), -- 전화번호
addr VARCHAR2(100), -- 주소
logtime DATE -- 생성일자
);


image


webapp/

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
	<h1>index</h1>
	<br>
	<a href="/08_member/member/writeForm.jsp"> 회원가입 </a>
	<br><br>
	<a href="/08_member/member/loginForm.jsp"> 로그인 </a>
</body>
</html>

member


writeForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>writeForm</title>
<style type="text/css">
table, tr, td {
	border: 1px solid black;
	border-collapse: collapse;
	padding: 10px;
}

.title {
	width: 120px;
	text-align: center;
}
</style>
<script type="text/javascript" src="../script/memberScript.js?after"></script>
</head>
<body>
	<h2>회원 가입</h2>
	<br>
	<form name="writeForm" action="write.jsp" method="post">
		<table>
			<tr>
				<td class="title">이름</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td class="title">I D</td>
				<td><input type="text" name="id" autocomplete="nope"> &nbsp; <input type="button" value="중복체크" onclick="checkId()" /></td>
			</tr>
			<tr>
				<td class="title">P W</td>
				<td><input type="password" name="pwd"></td>
			</tr>
			<tr>
				<td class="title">PWC</td>
				<td><input type="password" name="repwd"></td>
			</tr>
			<tr>
				<td class="title">성별</td>
				<td><input type="radio" name="gender" value="M" checked /><label
					for="genderM"></label> <input type="radio" name="gender"
					value="F"><label for="genderF"></label></td>
			</tr>
			<tr>
				<td class="title">E-mail</td>
				<td><input type="text" name="email" size="10"> @ <select
					name="domain">
						<option value="naver.com">naver.com</option>
						<option value="gmain.com">gmain.com</option>
				</select></td>
			</tr>
			<tr>
				<td class="title">핸드폰</td>
				<td><input type="text" name="tel_1" size="3" maxlength="3">
					- <input type="text" name="tel_2" size="4" maxlength="4"> -
					<input type="text" name="tel_3" size="4" maxlength="4"></td>
			</tr>
			<tr>
				<td class="title">주소</td>
				<td><input type="text" name="addr" size="50"></td>
			</tr>
			<tr>
				<td class="title" colspan="2"><input type="button" value="회원가입"
					onclick="checkWrite()" /> &nbsp; <input type="reset" value="다시작성" />
					&nbsp;</td>
			</tr>
		</table>
	</form>
</body>
</html>

write.jsp

<%@page import="member.dao.MemberDAO"%>
<%@page import="member.dto.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String repwd = request.getParameter("repwd");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String domain = request.getParameter("domain");
String tel_1 = request.getParameter("tel_1");
String tel_2 = request.getParameter("tel_2");
String tel_3 = request.getParameter("tel_3");
String tel = tel_1 + "-" + tel_2 + "-" + tel_3;
String addr = request.getParameter("addr");

MemberDTO memberDTO = new MemberDTO();
memberDTO.setName(name);
memberDTO.setId(id);
memberDTO.setPwd(pwd);
memberDTO.setGender(gender);
memberDTO.setEmail(email);
memberDTO.setDomain(domain);
memberDTO.setTel(tel);
memberDTO.setAddr(addr);

MemberDAO memberDAO = new MemberDAO();
int su = memberDAO.write(memberDTO);
String msg = "";
if (su == 0)
	msg = "실패";
else
	msg = "회원가입 성공";
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>write</title>
</head>
<body>
	<h2>회원가입 확인</h2>
	<br>
	<p>
		<strong><%=msg%></strong>
	</p>
	<input type="button" value="main" onclick="location.href='/08_member/index.jsp'"/>
</body>
</html>

checkId.jsp

<%@page import="member.dao.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String id = request.getParameter("id");

MemberDAO memberDAO = new MemberDAO();
boolean exist = memberDAO.isExistId(id);


%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>checkId.jsp</title>
</head>
<body>
	<h2>ID 중복 확인</h2>
	<form action="checkId.jsp" method="post">
		<%if(exist){ %>
			<p><%=id %> 사용중인 id </p>
			<br>
			<label>아이디 : </label><input type="text" name="id"/> &nbsp; <input type="submit" value="중복체크"/>
		<%} else { %>
			<p><%=id %> 사용 가능 id</p>
			<br>
			<input type="button" value="사용" onclick="checkIdClose()"/>
		<%} %>
	</form>
	<script type="text/javascript">
		function checkIdClose() {
			opener.writeForm.id.value="<%=id%>";
			window.close();
			opener.writeForm.pwd.focus();
		}
	</script>
</body>
</html>


loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginForm</title>
<style type="text/css">
table, th, td {
	border: 1px solid black;
	border-collapse: collapse;
}
tr:last-child > td{
	text-align: center;
}
th, td {
	padding: 4px;
}
.title {
	width: 120px;
}
</style>
<script type="text/javascript" src="../script/memberScript.js?after"></script>
</head>
<body>
	<h2>로그인</h2>
	<form name="loginForm" action="login.jsp" method="post">
		<table>
			<tr>
				<th class="title"> 아이디 </th>
				<td><input type="text" name="id"/></td>
			</tr>
			<tr>
				<th class="title"> 비밀번호 </th>
				<td><input type="password" name="pwd"/></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="button" value="로그인" onclick="login()"/> &nbsp;
					<input type="button" value="회원가입" onclick="location.href='writeForm.jsp'"/>
				</td>
			</tr>
		</table>
		
	</form>
</body>
</html>

login.jsp

<%@page import="java.net.URLEncoder"%>
<%@page import="member.dao.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%--  
	URL 인코딩
	- URI 에서 사용할 수 없는 문자를 인코딩
	URLEncoder.encode(변수명, "utf-8")
--%>
<%
request.setCharacterEncoding("utf-8");    
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
out.print(id + " - " + pwd);

MemberDAO memberDAO = new MemberDAO();

String name = memberDAO.login(id,pwd);
if(name!="" || name!=null){
	response.sendRedirect("loginOk.jsp?id="+URLEncoder.encode(id, "utf-8") + "&name="+ URLEncoder.encode(name, "utf-8"));
} else {
	response.sendRedirect("loginFail.jsp");
}
%>

loginOk.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginOk.jsp</title>
<style type="text/css">
img {
	width: 300px;
	height: 200px; 
	cursor: pointer;
}
</style>
</head>
<body>
	<br>
	<img src="../image/home.jpg" onclick="location.href='/08_member/index.jsp'"/>
	<br>
	<h2>ID : <%=id %> - <%=name %> ㅎㅇ ㅋㅋ</h2>
	<br>
	<input type="button" value="main" onclick="location.href='/08_member/index.jsp'"/>
</body>
</html>

loginFail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginFail.jsp</title>
<style type="text/css">
button {
	width: 100px; 
	height: 30px;
}
</style>
</head>
<body>
	<br>
	<h2>아이디 또는 비밀번호가 틀렸습니다</h2>
	<br>
	<button onclick="location.href='/08_member/index.jsp'"> main </button> &nbsp;
	<button onclick="location.href='/08_member/member/loginForm.jsp'"> 로그인 </button>
</body>
</html>

script


memberScript.js

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginFail.jsp</title>
<style type="text/css">
button {
	width: 100px; 
	height: 30px;
}
</style>
</head>
<body>
	<br>
	<h2>아이디 또는 비밀번호가 틀렸습니다</h2>
	<br>
	<button onclick="location.href='/08_member/index.jsp'"> main </button> &nbsp;
	<button onclick="location.href='/08_member/member/loginForm.jsp'"> 로그인 </button>
</body>
</html>

java/member/


MemberDTO

package member.dto;
/*
CREATE TABLE member(
name VARCHAR2(30) NOT NULL,  -- 이름
id VARCHAR2(30) PRIMARY KEY, -- id
pwd VARCHAR2(30) NOT NULL,   -- 비밀번호
gender VARCHAR2(3),          -- 성별
email VARCHAR2(20),          -- 이메일
domain VARCHAR2(20),         -- domain
tel VARCHAR2(13),            -- 전화번호
addr VARCHAR2(100),          -- 주소
logtime DATE                 -- 생성일자
);
 */
public class MemberDTO {
	private String name;
	private String id;
	private String pwd;
	private String gender;
	private String email;
	private String domain;
	private String tel;
	private String addr;
	public MemberDTO() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDomain() {
		return domain;
	}
	public void setDomain(String domain) {
		this.domain = domain;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
}

MemberDAO

package member.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import member.dto.MemberDTO;
import oracle.jdbc.proxy.annotation.Pre;

public class MemberDAO {

	// 연결 정보
	private String driver = "oracle.jdbc.OracleDriver";
	private String url = "jdbc:oracle:thin:@localhost:1521:xe";
	private String id = "dbtest";
	private String pwd = "a1234";
	
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private ResultSet res = null;
	
	public MemberDAO() {
		try {
			Class.forName(driver);
			//System.out.println("로딩 성공");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() {
		try {
			// DB 연결 객체 생성
			con = DriverManager.getConnection(url, id, pwd);
			//System.out.println("연결 성공!!");
		} catch (Exception e) {
			//System.out.println("연결 실패~");
			e.printStackTrace();
		}
		return con;
	}// getConnection() end
	
	
	public int write(MemberDTO dto) {
		Connection con = null;
		PreparedStatement pstmt = null;
		int res = 0;
		try {
			String sql ="INSERT INTO MEMBER VALUES(?,?,?,?,?,?,?,?,SYSDATE)";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getId());
			pstmt.setString(3, dto.getPwd());
			pstmt.setString(4, dto.getGender());
			pstmt.setString(5, dto.getEmail());
			pstmt.setString(6, dto.getDomain());
			pstmt.setString(7, dto.getTel());
			pstmt.setString(8, dto.getAddr());
			res = pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!=null) con.close();
				if(pstmt!=null) pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return res;
	}// write() end
	
	public boolean isExistId(String id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		boolean ex = false;
		try {
			String sql = "SELECT * FROM member WHERE ID=?";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			res = pstmt.executeQuery();
			if(res.next()) {
				ex = true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res!=null) res.close();
				if(pstmt!=null) pstmt.close();
				if(con!=null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ex;
	}
	
	// 로그인
	public String login(String id, String pwd) {
		
		String name ="";
		try {
			String sql = "SELECT*FROM member WHERE id=? and pwd=?";
			con = this.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2, pwd);
			res = pstmt.executeQuery();
			if(res.next()) {
				name = res.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res!= null) res.close();
				if(pstmt!= null) pstmt.close();
				if(con!= null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return name;
	}
}


09_Cookie_Session


업로드중..

< Cookie_Session >

쿠키 ( cookie )

  • HTTP 프로토콜은 웹 브라우저(클라이언트)의 요청에 대한 응답을 하고 나면,
    해당 클라이언트의 연결을 지속하고 있지 않는다.
    상태가 없는 프로토콜을 위해 상태를 지속시키기 위한 방법이다

  • 파일로 저장되기 때문에 브라우저가 종료되어도 생존기간 동안 데이터가 유지 됨

Cookie 사용

  • Cookie 객체 생성

    Cookie cookie = new Cookie( String name, String value );

  • Cookie 를 response 객체에 추가

    response.addCookie ( cookie );

  • requset 객체를 사용해서 쿠키 읽기
    Cookie[] cookies = resquest.getCookies();

Cookie 사용하는 순서

    1. Cookie 객체 생성
    2. Cookie 설정
    3. 웹 브라우저에 생성된 Cookie 전송

웹 브라우저에 저장된 Cookie 를 사용하는 절차

    1. 웹 브라우저에서 Cookie 얻기
    2. Cookie 는 이름, 값의 쌍으로 된 배열 형태로 반환됨
      반환된 Cookie 의 배열에서 Cookie 이름을 가져온다
    3. Cookie 이름을 통해 Cookie 에 설정된 값을 가져온다

Session

  • 웹 컨테이너에 상태를 유지하기 위한 정보 저장

    웹 브라우저 당 1개씩 생성

  • 웹 서버는 각각의 웹 브라우저로 부터 발생한 요청에 대해서 특별한 식별자를 부여함

  • 브라우저 종료시 데이터 소실

세션 사용

  • session 속성 설정 : setAttribute()
  • session 속성 사용 : getAttribute()
  • session 속성 삭제 : removeAttribute()
  • session 무효화 : invalidate()

webapp/


cookieMake.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String cookieName = "ckTest";
String cookieValue = "test_cookie";
Cookie myCookie = new Cookie(cookieName, cookieValue); // 쿠키 생성
myCookie.setMaxAge(3); // 쿠키 지속 시간 : 초단위로 지정(0 : 즉시 삭제, -1 : 브라우저 종료시 삭제)
response.addCookie(myCookie);
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookieMake.jsp</title>
</head>
<body>
	<h1>쿠키 생성 페이지</h1>
	<br>
	<p><%=cookieName %> 쿠키가 생성 되었습니다</p>
	<br>
	<form action="cookieUse.jsp" method="post"/>
		<input type="submit" value="확인"/>
	</form>
</body>
</html>

cookieUse.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String myCookie = null;
String myValue = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
	for(int i=0; i<cookies.length; i++){
		if(cookies[i].getName().equals("ckTest")){
			myCookie = cookies[i].getName(); 
			myValue = cookies[i].getValue(); 
		}
	}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookieUse.jsp</title>
</head>
<body>
	<h1>쿠키 확인</h1>
	<br>
	<p>쿠키 확인 : <%=myCookie %></p>
	<p>쿠키 확인 : <%=myValue %></p>
</body>
</html>

sessionForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sessionForm.jsp</title>
</head>
<body>
	<h1>session Form</h1>
	<br>
	<form action="sessionPro.jsp" method="post">
		아이디 : <input type="text" name="id"/>
		<br><br>
		비밀번호 : <input type="password" name="pwd"/>
		<br><br>
		<input type="submit" value="확인"/>
	</form>
</body>
</html>

sessionPro.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");

session.setAttribute("userid", id);
session.setAttribute("userpwd", pwd);

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>session 설정 및 사용</h1>
	<br>
	<p>ID = <%=(String)session.getAttribute("userid") %></p>
	<p>PW = <%=(String)session.getAttribute("userpwd") %></p>
</body>
</html>


10_MemberCookie


login.jsp

<%@page import="java.net.URLEncoder"%>
<%@page import="member.dao.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%--  
	URL 인코딩
	- URI 에서 사용할 수 없는 문자를 인코딩
	URLEncoder.encode(변수명, "utf-8")
--%>
<%
request.setCharacterEncoding("utf-8");    
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");

MemberDAO memberDAO = new MemberDAO();

String name = memberDAO.login(id,pwd);
if(name!="" || name!=null){
	Cookie cookieId = new Cookie("memberId", URLEncoder.encode(id, "utf-8"));
	cookieId.setMaxAge(10);
	cookieId.setPath("/");// 쿠키 범위 지정
	response.addCookie(cookieId);
	
	Cookie cookieName = new Cookie("memberName", URLEncoder.encode(name, "utf-8"));
	cookieName.setMaxAge(-1);
	cookieName.setPath("/");
	response.addCookie(cookieName);
	
	response.sendRedirect("loginOk.jsp");
} else {
	response.sendRedirect("loginFail.jsp");
}
%>

loginOk.jsp

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
// Cookie 값 사용
String id = null;
String name = null;

Cookie[] cookies = request.getCookies();
if(cookies !=null){
	for(int i=0; i<cookies.length; i++){
		if(cookies[i].getName().equals("memberId")){
			id = URLDecoder.decode(cookies[i].getValue(),"utf-8"); 
		}else if(cookies[i].getName().equals("memberName")){
			name = URLDecoder.decode(cookies[i].getValue(),"utf-8"); 
		}
	}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginOk.jsp</title>
<style type="text/css">
img {
	width: 300px;
	height: 200px; 
	cursor: pointer;
}
</style>
</head>
<body>
	<br>
	<img src="../image/home.jpg" onclick="location.href='/10_MemberCookie/index.jsp'"/>
	<br>
	<h2>ID : <%=id %> - <%=name %> 이게 이름? ㅋㅋ</h2>
	<br>
	<input type="button" value="main" onclick="location.href='/10_MemberCookie/index.jsp'"/>
</body>
</html>

10_MemberSession

index.jsp

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
String id = (String)session.getAttribute("memberId");
String name = (String)session.getAttribute("memberName");
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
	<h1>index</h1>
	<br>
	<%if(id==null||id==""){ %>
		<a href="member/writeForm.jsp"> 회원가입 </a>
		<br><br>
		<a href="member/loginForm.jsp"> 로그인 </a>
	<%}else { %>
		<a href="member/logout.jsp">로그아웃</a>
		<br><br>
		<a href="member/modifyForm.jsp"> 회원정보 수정</a>
	<%} %>
</body>
</html>

MemberDAO.java

package member.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import member.dto.MemberDTO;
import oracle.jdbc.proxy.annotation.Pre;

public class MemberDAO {

	// 연결 정보
	private String driver = "oracle.jdbc.OracleDriver";
	private String url = "jdbc:oracle:thin:@localhost:1521:xe";
	private String id = "dbtest";
	private String pwd = "a1234";
	
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private ResultSet res = null;
	
	public MemberDAO() {
		try {
			Class.forName(driver);
			//System.out.println("로딩 성공");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() {
		try {
			// DB 연결 객체 생성
			con = DriverManager.getConnection(url, id, pwd);
			//System.out.println("연결 성공!!");
		} catch (Exception e) {
			//System.out.println("연결 실패~");
			e.printStackTrace();
		}
		return con;
	}// getConnection() end
	
	
	public int write(MemberDTO dto) {
		Connection con = null;
		PreparedStatement pstmt = null;
		int res = 0;
		try {
			String sql ="INSERT INTO MEMBER VALUES(?,?,?,?,?,?,?,?,SYSDATE)";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getId());
			pstmt.setString(3, dto.getPwd());
			pstmt.setString(4, dto.getGender());
			pstmt.setString(5, dto.getEmail());
			pstmt.setString(6, dto.getDomain());
			pstmt.setString(7, dto.getTel());
			pstmt.setString(8, dto.getAddr());
			res = pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!=null) con.close();
				if(pstmt!=null) pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return res;
	}// write() end
	
	public boolean isExistId(String id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		boolean ex = false;
		try {
			String sql = "SELECT * FROM member WHERE ID=?";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			res = pstmt.executeQuery();
			if(res.next()) {
				ex = true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res!=null) res.close();
				if(pstmt!=null) pstmt.close();
				if(con!=null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ex;
	}
	
	// 로그인
	public String login(String id, String pwd) {
		
		String name ="";
		try {
			String sql = "SELECT*FROM member WHERE id=? and pwd=?";
			con = this.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2, pwd);
			res = pstmt.executeQuery();
			if(res.next()) {
				name = res.getString("name");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res!= null) res.close();
				if(pstmt!= null) pstmt.close();
				if(con!= null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return name;
	}
	
	// 회원정보 가져오기
	public MemberDTO bring(String id) {
		MemberDTO data = null;
		try {
			data = new MemberDTO();
			String sql = "SELECT*FROM member WHERE id=?";
			con = this.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			res = pstmt.executeQuery();
			if(res.next()) {
				data.setName(res.getString("name"));
				data.setId(res.getString("id"));
				data.setPwd(res.getString("pwd"));
				data.setGender(res.getString("gender"));
				data.setEmail(res.getString("email"));
				data.setDomain(res.getString("domain"));
				data.setTel(res.getString("tel"));
				data.setAddr(res.getString("addr"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(res!=null)res.close();
				if(pstmt!=null)pstmt.close();
				if(con!=null)con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return data;
	}
	
	public int update(MemberDTO dto, String id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		int res = 0;
		try {
			String sql ="UPDATE member SET name=?,pwd=?,gender=?,email=?,domain=?,tel=?,addr=? where id=?";
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getPwd());
			pstmt.setString(3, dto.getGender());
			pstmt.setString(4, dto.getEmail());
			pstmt.setString(5, dto.getDomain());
			pstmt.setString(6, dto.getTel());
			pstmt.setString(7, dto.getAddr());
			pstmt.setString(8, id);
			res = pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!=null) con.close();
				if(pstmt!=null) pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return res;
	}
}

memberScript.js


// 회원 정보 입력 확인
function checkWrite(){
	if(document.writeForm.name.value==""){
		alert("이름을 입력하세요");
		document.writeForm.name.focus();
	}else if(document.writeForm.id.value==""){
		alert("아이디를 입력하세요");
		document.writeForm.id.focus();
	}else if(document.writeForm.pwd.value==""){
		alert("비밀번호을 입력하세요");
		document.writeForm.pwd.focus();
	}else if(document.writeForm.pwd.value!=document.writeForm.repwd.value){
		alert("비밀번호를 확인하세요");
		document.writeForm.repwd.focus();
	}else if(document.writeForm.email.value==""){
		alert("이메일을 입력하세요");
		document.writeForm.email.focus();
	}else if(document.writeForm.tel_1.value==""){
		alert("전화번호를 모두 입력하세요");
		document.writeForm.tel_1.focus();
	}else if(document.writeForm.tel_2.value==""){
		alert("전화번호를 모두 입력하세요");
		document.writeForm.tel_2.focus();
	}else if(document.writeForm.tel_3.value==""){
		alert("전화번호를 모두 입력하세요");
		document.writeForm.tel_3.focus();
	}else if(document.writeForm.addr.value==""){
		alert("주소를 입력하세요");
		document.writeForm.addr.focus();
	} else {
		document.writeForm.submit();
	}
}

// ID 중복 확인
function checkId(){
	let uid = document.writeForm.id.value;
	if(uid == "" || uid ==0) {
		alert("아이디를 입력하세요~");
		document.writeForm.id.focus();
	}else {
		window.open("./checkId.jsp?id="+uid,'','left=200, top=200,width=500,height=300');
	}
}
// 로그인
function login(){
	if(document.loginForm.id.value==""){
		alert("아이디 입력");
		document.loginForm.id.focus();
	}
	else if(document.loginForm.pwd.value==""){
		alert("비밀번호 입력");
		document.loginForm.pwd.focus();
	} else {
		document.loginForm.submit();
	}
}

//정보 수정
function checkModify(){
	if(document.modifyForm.name.value==""){
		alert("이름을 입력하세요");
		document.modifyForm.name.focus();
	}else if(document.modifyForm.id.value==""){
		alert("아이디를 입력하세요");
		document.modifyForm.id.focus();
	}else if(document.modifyForm.pwd.value==""){
		alert("비밀번호을 입력하세요");
		document.modifyForm.pwd.focus();
	}else if(document.modifyForm.pwd.value!=document.modifyForm.repwd.value){
		alert("비밀번호를 확인하세요");
		document.modifyForm.repwd.focus();
	}else if(document.modifyForm.email.value==""){
		alert("이메일을 입력하세요");
		document.modifyForm.email.focus();
	}else if(document.modifyForm.tel_1.value==""){
		alert("전화번호를 모두 입력하세요");
		document.modifyForm.tel_1.focus();
	}else if(document.modifyForm.tel_2.value==""){
		alert("전화번호를 모두 입력하세요");
		document.modifyForm.tel_2.focus();
	}else if(document.modifyForm.tel_3.value==""){
		alert("전화번호를 모두 입력하세요");
		document.modifyForm.tel_3.focus();
	}else if(document.modifyForm.addr.value==""){
		alert("주소를 입력하세요");
		document.modifyForm.addr.focus();
	}else {
		document.modifyForm.submit();
	}
}

login.jsp

<%@page import="member.dao.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%--  
	URL 인코딩
	- URI 에서 사용할 수 없는 문자를 인코딩
	URLEncoder.encode(변수명, "utf-8")
--%>
<%
  
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");

MemberDAO memberDAO = new MemberDAO();
String name = memberDAO.login(id,pwd);

if(name!=""){
	// session 설정
	session.setAttribute("memberId", id);
	session.setAttribute("memberName", name);
	
	response.sendRedirect("loginOk.jsp");
} else {
	response.sendRedirect("loginFail.jsp");
}
%>

loginOk.jsp

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String id = (String)session.getAttribute("memberId");
String name = (String)session.getAttribute("memberName");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginOk.jsp</title>
<style type="text/css">
img {
	width: 300px;
	height: 200px; 
	cursor: pointer;
}
</style>
</head>
<body>
	<br>
	<img src="../image/home.jpg" onclick="location.href='../index.jsp'"/>
	<br>
	<h2>ID : <%=id %> - <%=name %> 이게 이름? ㅋㅋ</h2>
	<br>
	<input type="button" value="main" onclick="location.href='../index.jsp'"/>
</body>
</html>

modifyForm.jsp

<%@page import="member.dto.MemberDTO"%>
<%@page import="member.dao.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String id = (String)session.getAttribute("memberId");
// 현재 접속된 id 데이터 가져오기
MemberDAO memberDAO = new MemberDAO();
MemberDTO data = new MemberDTO();
data = memberDAO.bring(id);
String tel_1 = data.getTel().substring(0,3);
String tel_2 = data.getTel().substring(4,8);
String tel_3 = data.getTel().substring(9);
%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>modifyForm.jsp</title>
<style type="text/css">
table, tr, td {
	border: 1px solid black;
	border-collapse: collapse;
	padding: 10px;
}

.title {
	width: 120px;
	text-align: center;
}
</style>
<script type="text/javascript" src="../script/memberScript.js?after"></script>
</head>
<body>
	<h2>회원정보 수정</h2>
	<br>
	<form name="modifyForm" action="modify.jsp" method="post">
		<table>
			<tr>
				<td class="title">이름</td>
				<td><input type="text" name="name" value="<%=data.getName() %>" autocomplete="nope"></td>
			</tr>
			<tr>
				<td class="title">I D</td>
				<td><input type="text" name="id" value="<%=data.getId() %>" readonly/></td>
			</tr>
			<tr>
				<td class="title">P W</td>
				<td><input type="text" name="pwd" value="<%=data.getPwd() %>" ></td>
			</tr>
			<tr>
				<td class="title">PWC</td>
				<td><input type="text" name="repwd"></td>
			</tr>
			<tr>
				<td class="title">성별</td>
				<td>
				<%if(data.getGender().equals("M")) {%>
					<input type="radio" name="gender" value="M" checked/><label for="genderM"></label> 
					<input type="radio" name="gender" value="F"/><label for="genderF"></label>
				<%}else {%>
					<input type="radio" name="gender" value="M"/><label for="genderM"></label> 
					<input type="radio" name="gender" value="F" checked /><label for="genderF"></label>
				<%} %>
				</td>
			</tr>
			<tr>
				<td class="title">E-mail</td>
				<td>
					<input type="text" name="email" size="10" value="<%=data.getEmail() %>" autocomplete="nope"/> @ 
					<input type="text" name="domain" value="<%=data.getDomain() %>"/>
				</td>
			</tr>
			<tr>
				<td class="title">핸드폰</td>
				<td><input type="text" name="tel_1" value="<%=tel_1 %>" size="3" maxlength="3">
					- <input type="text" name="tel_2" value="<%=tel_2 %>" size="4" maxlength="4"> -
					<input type="text" name="tel_3" value="<%=tel_3 %>" size="4" maxlength="4"></td>
			</tr>
			<tr>
				<td class="title">주소</td>
				<td><input type="text" name="addr" value="<%=data.getAddr() %>" size="50"></td>
			</tr>
			<tr>
				<td class="title" colspan="2">
					<input type="button" value="수정완료" onclick="checkModify()"/> &nbsp; 
					<input type="reset" value="다시작성"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

modify.jsp

<%@page import="member.dao.MemberDAO"%>
<%@page import="member.dto.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String repwd = request.getParameter("repwd");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String domain = request.getParameter("domain");
String tel_1 = request.getParameter("tel_1");
String tel_2 = request.getParameter("tel_2");
String tel_3 = request.getParameter("tel_3");
String tel = tel_1 + "-" + tel_2 + "-" + tel_3;
String addr = request.getParameter("addr");

MemberDTO memberDTO = new MemberDTO();
MemberDAO memberDAO = new MemberDAO();

memberDTO.setName(name);
memberDTO.setId(id);
memberDTO.setPwd(pwd);
memberDTO.setGender(gender);
memberDTO.setEmail(email);
memberDTO.setDomain(domain);
memberDTO.setTel(tel);
memberDTO.setAddr(addr);

int su = memberDAO.update(memberDTO, id);
String msg = "";
if (su == 0)
	msg = "실패";
else
	msg = "회원정보 수정 성공";
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>modify</title>
</head>
<body>
	<h2>회원정보 수정</h2>
	<br>
	<p>
		<strong><%=msg%></strong>
	</p>
	<input type="button" value="main" onclick="location.href='../index.jsp'"/>
</body>
</html>
profile
Fintech

0개의 댓글