Main funtions
1. Connection Pool
2. Action tags : <jsp:useBean...> / <jsp:setProperty...>
3. DTO, DAO class
4. Session
Log in to Oracle, and create a new account(totoro) for a new project.
create user totoro identified by totoro123;
grant connect, resource to totoro;
Create a new table with the new account.
create table member(
id varchar2(20) primary key,
passwd varchar2(20) not null,
name varchar2(20) not null,
jumin1 varchar2(6) not null,
jumin2 varchar2(7) not null,
mailid varchar2(30),
domain varchar2(30),
tel1 varchar2(5),
tel2 varchar2(5),
tel3 varchar2(5),
phone1 varchar2(5),
phone2 varchar2(5),
phone3 varchar2(5),
post varchar2(10),
address varchar2(200),
gender varchar2(20),
hobby varchar2(50),
intro varchar2(2000),
register timestamp );
DTO Class
// DTO(Data Transfer Object)
package member;
import java.sql.Timestamp;
public class MemberDTO {
private String id;
private String passwd;
private String name;
private String jumin1;
private String jumin2;
private String mailid;
private String domain;
private String tel1;
private String tel2;
private String tel3;
private String phone1;
private String phone2;
private String phone3;
private String post;
private String address;
private String gender;
private String hobby;
private String intro;
private Timestamp register;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJumin1() {
return jumin1;
}
public void setJumin1(String jumin1) {
this.jumin1 = jumin1;
}
public String getJumin2() {
return jumin2;
}
public void setJumin2(String jumin2) {
this.jumin2 = jumin2;
}
public String getMailid() {
return mailid;
}
public void setMailid(String mailid) {
this.mailid = mailid;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getTel1() {
return tel1;
}
public void setTel1(String tel1) {
this.tel1 = tel1;
}
public String getTel2() {
return tel2;
}
public void setTel2(String tel2) {
this.tel2 = tel2;
}
public String getTel3() {
return tel3;
}
public void setTel3(String tel3) {
this.tel3 = tel3;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getPhone3() {
return phone3;
}
public void setPhone3(String phone3) {
this.phone3 = phone3;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public Timestamp getRegister() {
return register;
}
public void setRegister(Timestamp register) {
this.register = register;
}
}
memberform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 가입 폼</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://dmaps.daum.net/map_js_init/postcode.v2.js"></script>
<script>
function openDaumPostcode() {
new daum.Postcode({
oncomplete : function(data) {
// 팝업에서 검색결과 항목을 클릭했을때 실행할 코드를 작성하는 부분.
// 우편번호와 주소 정보를 해당 필드에 넣고, 커서를 상세주소 필드로 이동한다.
// document.getElementById('join_zip1').value = data.postcode1;
document.getElementById('post').value = data.zonecode;
document.getElementById('address').value = data.address;
}
}).open();
}
</script>
<!-- 외부 자바스크립트 파일 불러오기 -->
<script src="member.js"></script>
</head>
<body>
<form method="post" action="member.jsp">
<table border=1 width=450 align="center">
<caption>회원 가입 폼</caption>
<tr><td>ID</td>
<td><input type=text autofocus="autofocus" id="id" name="id">
<input type=button value="ID중복검사" id="idcheck">
<div id="myid"></div>
</td>
</tr>
<tr><td>비밀번호</td>
<td><input type=password id="passwd" name="passwd">
</td>
</tr>
<tr><td>성명</td>
<td><input type=text id="name" name="name"></td>
</tr>
<tr><td>주민번호</td>
<td><input type=text size=6 maxlength=6 id="jumin1" name="jumin1">-
<input type=text size=7 maxlength=7 id="jumin2" name="jumin2">
</td>
</tr>
<tr><td>E-Mail</td>
<td><input type=text size=10 id="mailid" name="mailid">@
<input type=text size=10 id="domain" name="domain">
<select id="email">
<option value="">직접입력</option>
<option value="naver.com">네이버</option>
<option value="daum.net">다음</option>
<option value="nate.com">네이트</option>
<option value="gmail.com">gmail</option>
</select>
</td>
</tr>
<tr><td>전화번호</td>
<td><input type=text size=4 maxlength=4 id="tel1" name="tel1">-
<input type=text size=4 maxlength=4 id="tel2" name="tel2">-
<input type=text size=4 maxlength=4 id="tel3" name="tel3">-
</td>
</tr>
<tr><td>핸드폰</td>
<td><select id="phone1" name="phone1">
<option value="">번호선택</option>
<option value="010">010</option>
<option value="011">011</option>
<option value="016">016</option>
<option value="018">018</option>
<option value="019">019</option>
</select>-
<input type=text size=4 maxlength=4 id="phone2" name="phone2">-
<input type=text size=4 maxlength=4 id="phone3" name="phone3">
</td>
</tr>
<tr><td>우편번호</td>
<td><input type=text size=5 maxlength=5 id="post" name="post">
<input type=button value="우편검색" onClick="openDaumPostcode()">
</td>
</tr>
<tr><td>주소</td>
<td><input type=text size=45 id="address" name="address"></td>
</tr>
<tr><td>성별</td>
<td><input type=radio id="male" name="gender" value="남자">남자
<input type=radio id="female" name="gender" value="여자">여자
</td>
</tr>
<tr><td>취미</td>
<td><input type=checkbox id="h1" name="hobby" value="공부">공부
<input type=checkbox id="h2" name="hobby" value="게임">게임
<input type=checkbox id="h3" name="hobby" value="등산">등산
<input type=checkbox id="h4" name="hobby" value="낚시">낚시
<input type=checkbox id="h5" name="hobby" value="쇼핑">쇼핑
</td>
</tr>
<tr><td>자기소개</td>
<td><textarea rows=5 cols=50 id="intro" name="intro"
placeholder="자기소개를 100자 이내로 입력하세요"></textarea>
</td>
</tr>
<tr><td colspan=2 align=center>
<input type=submit value="회원가입">
<input type=reset value="취소">
</td>
</tr>
</table>
</form>
</body>
</html>
member.js (Validation Check 유효성검사)
$(document).ready(function(){
// ID중복검사 (popup 창으로 처리)
// $("#idcheck").click(function(){
// if($("#id").val()==""){
// alert("ID를 입력하세요.");
// $("#id").focus();
// return false;
// }else{
// // open("팝업창에 실행될 문서명","윈도우이름","옵션")
// var ref="idcheck.jsp?id="+$("#id").val();
// window.open(ref,"mywin","width=250,height=150");
// }
// });
// ajax로 ID중복 검사
$("#idcheck").click(function(){
if($("#id").val() == ""){
alert("ID를 입력 하세요.");
$("#id").focus();
return false;
}else{
var id = $("#id").val(); // 사용자가 입력한 ID
$.ajax({
type : "post", // get, post, put, delete
url : "idcheck1.jsp",
data : {"id" : id}, // json
success : function(data){ // callback함수로 결과를 돌려 받는다.
alert(data);
if(data == 1){ // 중복 ID
$("#myid").html("<font color=red>중복 ID입니다.</font>");
$("#id").val("").focus();
}else{ // 사용 가능한 ID
$("#myid").html("<font color=red>사용 가능한 ID입니다.</font>");
$("#passwd").focus();
}
}
}); // $.ajax() end
} // else end
}); // click end
// 주민번호 뒷자리로 포커스 이동
$("#jumin1").keyup(function(){
if($("#jumin1").val().length == 6)
$("#jumin2").focus();
});
// 도메인 선택
$("#email").change(function(){
if($("#email").val()==""){ // 직접 입력 선택
$("#domain").attr("readOnly", false);
$("#domain").val("").focus();
}else{ // 도메인을 선택
$("#domain").val($("#email").val());
$("#domain").attr("readOnly","readOnly");
}
});
// 유효성 검사
$("form").submit(function(){
if($("#id").val() == ""){
alert("ID를 입력하세요.");
$("#id").focus();
return false;
}
if($("#passwd").val()==""){
alert("비밀번호를 입력하세요.");
$("#passwd").focus();
return false;
}
if($("#name").val()==""){
alert("이름을 입력하세요.");
$("#name").focus();
return false;
}
if($("#jumin1").val()==""){
alert("주민번호 앞자리를 입력하세요.");
$("#jumin1").focus();
return false;
}
if($("#jumin1").val().length != 6){
alert("주민번호 앞자리 6자리를 입력하세요.");
$("#jumin1").val("").focus();
return false;
}
// isNaN() : 문자가 포함되면 true를 리턴하는 함수
if(isNaN($("#jumin1").val())){
alert("숫자만 입력하세요.");
$("#jumin1").val("").focus();
return false;
}
if($("#jumin2").val()==""){
alert("주민번호 뒷자리를 입력하세요.");
$("#jumin2").focus();
return false;
}
if($("#jumin2").val().length != 7){
alert("주민번호 뒷자리 7자리를 입력하세요.");
$("#jumin2").val("").focus();
return false;
}
// isNaN() : 문자가 포함되면 true를 리턴하는 함수
if(isNaN($("#jumin2").val())){
alert("숫자만 입력하세요.");
$("#jumin2").val("").focus();
return false;
}
if($("#mailid").val()==""){
alert("EMail주소를 입력하세요.");
$("#mailid").focus();
return false;
}
if($("#domain").val()==""){
alert("도메인을 입력하세요.");
$("#domain").focus();
return false;
}
if($("#tel1").val()==""){
alert("전화번호 앞자리를 입력하세요.");
$("#tel1").focus();
return false;
}
if(isNaN($("#tel1").val())){
alert("숫자만 입력하세요.");
$("#tel1").val("").focus();
return false;
}
if($("#tel2").val()==""){
alert("전화번호 중간자리를 입력하세요.");
$("#tel2").focus();
return false;
}
if(isNaN($("#tel2").val())){
alert("숫자만 입력하세요.");
$("#tel2").val("").focus();
return false;
}
if($("#tel3").val()==""){
alert("전화번호 끝자리를 입력하세요.");
$("#tel3").focus();
return false;
}
if(isNaN($("#tel3").val())){
alert("숫자만 입력하세요.");
$("#tel3").val("").focus();
return false;
}
if($("#phone1").val()==""){
alert("핸드폰 앞자리를 선택 하세요.");
return false;
}
if($("#phone2").val()==""){
alert("핸드폰 중간자리를 입력하세요.");
$("#phone2").focus();
return false;
}
if(isNaN($("#phone2").val())){
alert("숫자만 입력하세요.");
$("#phone2").val("").focus();
return false;
}
if($("#phone3").val()==""){
alert("핸드폰 끝자리를 입력하세요.");
$("#phone3").focus();
return false;
}
if(isNaN($("#phone3").val())){
alert("숫자만 입력하세요.");
$("#phone3").val("").focus();
return false;
}
if($("#post").val()==""){
alert("우편번호를 입력하세요.");
$("#post").focus();
return false;
}
if($("#address").val()==""){
alert("주소를 입력하세요.");
$("#address").focus();
return false;
}
if($("#male").is(":checked")==false &&
$("#female").is(":checked")==false){
alert("남.여 중에서 1개를 선택 하세요");
return false;
}
/* var cnt=0;
if($("#h1").is(":checked")) cnt++;
if($("#h2").is(":checked")) cnt++;
if($("#h3").is(":checked")) cnt++;
if($("#h4").is(":checked")) cnt++;
if($("#h5").is(":checked")) cnt++;
if(cnt < 2){
alert("취미를 2개 이상 선택 하세요.");
return false;
} */
if($("input:checkbox[name='hobby']:checked").length < 2){
alert("취미를 2개 이상 선택 하세요.");
return false;
}
if($("#intro").val()==""){
alert("자기소개를 입력하세요.");
$("#intro").focus();
return false;
}
if($("#intro").val().length > 100){
alert("자기소개를 100자 이내로 입력하세요.");
$("#intro").focus();
return false;
}
}); // submit() end
}); // ready() end
member.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="member.MemberDAO"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="member" class="member.MemberDTO" />
<jsp:setProperty property="*" name="member"/>
<%
String[] hobby = request.getParameterValues("hobby");
String h = ""; // h = "공부-게임-등산-"
for(String h1 : hobby){
h += h1 + "-";
}
member.setHobby(h);
// DAO 객체 생성
MemberDAO dao = MemberDAO.getInstance();
int result = dao.insert(member); //insert SQL문을 실행할 메소드 호출
if(result == 1){ // 회원가입 성공
%>
<script>
alert("회원가입 성공");
location.href="login.html";
</script>
<% }else{ //회원가입 실패 %>
<script>
alert("회원가입 실패");
history.go(-1);
</script>
<% }%>
idcheck.jsp
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#close1").click(function(){ // 중복 ID인 경우의 닫기 버튼 클릭
opener.$("#id").val("").focus();
window.close();
});
$("#close2").click(function(){ // 사용 가능한 ID 인 경우의 닫기 버튼 클릭
opener.$("#passwd").focus();
window.close();
});
});
</script>
<%
String id = request.getParameter("id");
MemberDAO dao = MemberDAO.getInstance();
// id 중복검사 메소드 호출
int result = dao.memberAuth(id);
if (result == 1) { // 중복 ID : 1
%>
<%=id%>는 중복 ID 입니다.
<br>
<br>
<br>
<input type="button" value="Close" id="close1">
<%
} else { // 사용 가능한 ID : -1
%>
<%=id%>는 사용가능한 ID 입니다.
<br>
<br>
<br>
<input type="button" value="Close" id="close2">
<%
}
%>
idcheck1.jsp
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("id");
System.out.println("id="+id); // Console 창에 출력
MemberDAO dao = MemberDAO.getInstance();
int result = dao.memberAuth(id);
System.out.println("result="+result);
//result : 1(중복 ID)
//result : -1(사용가능한 ID)
// 웹브라우저에 출력되는 값이 callback함수로 리턴된다.
out.println(result);
%>
login.jsp
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="member" class="member.MemberDTO" />
<jsp:setProperty property="*" name="member" />
<%
MemberDAO dao = MemberDAO.getInstance();
int result = dao.memberCheck(member); // 회원인증 처리
if (result == 1) { // 로그인 성공
session.setAttribute("id", member.getId());
%>
<script>
alert("로그인 성공");
location.href = "main.jsp";
</script>
<%
} else {
%>
<script>
alert("로그인 실패");
hisroty.go(-1);
</script>
<%
}
%>
main.jsp(세션값이 있냐 없냐에 따라서 값이 달라짐)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = (String) session.getAttribute("id");
if (id != null) { //세션이 있는 경우(정상적으로 로그인)
%>
<%=id %>님 환영합니다. <br><br><br>
<a href="updateform.jsp">정보수정</a><br>
<a href="logout.jsp">로그아웃 </a><br>
<a href="deleteform.jsp">회원탈퇴</a><br>
<!-- <a href="list.jsp">글목록</a><br> -->
<%
} else { // 세션이 없는 경우(비정상적인 접속))
%>
<a href="memberform.html">회원가입</a><br>
<a href="loginform.html">로그인</a><br>
<%
}
%>
세션이 없을 때 When there is no session:
세션이 있을 때 Where there is session:
logout.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% //세션 삭제: 서버와 클라이언트의 연결이 끊어짐
session.invalidate();
%>
<script>
alert("Log out");
location.href="loginform.html";
</script>
updateform.jsp (memberform.html과 양식은 동일하지만 가입 시 등록한 정보가 입력되어있음)
<%@page import="member.MemberDTO"%>
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = (String) session.getAttribute("id");
MemberDAO dao = MemberDAO.getInstance();
// 한 사람의 상세정보 구해오기
MemberDTO member = dao.getMember(id);
// h[0] = "공부", h[1] ="게임",...
String[] h = member.getHobby().split("-");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 수정 폼</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://dmaps.daum.net/map_js_init/postcode.v2.js"></script>
<script>
function openDaumPostcode() {
new daum.Postcode({
oncomplete : function(data) {
// 팝업에서 검색결과 항목을 클릭했을때 실행할 코드를 작성하는 부분.
// 우편번호와 주소 정보를 해당 필드에 넣고, 커서를 상세주소 필드로 이동한다.
// document.getElementById('join_zip1').value = data.postcode1;
document.getElementById('post').value = data.zonecode;
document.getElementById('address').value = data.address;
}
}).open();
}
</script>
<!-- 외부 자바스크립트 파일 불러오기 -->
<script src="member.js"></script>
<script>
$(document).ready(function(){
<%for (String s : h) {%> // value 값이 s인 값을 구해와라
$("input:checkbox[name='hobby'][value='<%=s%>']").attr("checked", true);
<%}%>
});
</script>
</head>
<body>
<form method="post" action="update.jsp">
<input type="hidden" name="id" value="<%=member.getId() %>">
<table border=1 width=450 align="center">
<caption>회원 수정 폼</caption>
<tr>
<td>ID</td>
<td><%=member.getId()%></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type=password id="passwd" name="passwd"></td>
</tr>
<tr>
<td>성명</td>
<td><input type=text id="name" name="name"
value="<%=member.getName()%>"></td>
</tr>
<tr>
<td>주민번호</td>
<td><input type=text size=6 maxlength=6 id="jumin1"
name="jumin1" value="<%=member.getJumin1()%>">- <input
type=text size=7 maxlength=7 id="jumin2" name="jumin2"
value="<%=member.getJumin2()%>"></td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type=text size=10 id="mailid" name="mailid"
value="<%=member.getMailid()%>">@ <input type=text size=10
id="domain" name="domain" value="<%=member.getDomain()%>">
<select id="email">
<option value="">직접입력</option>
<option value="naver.com">네이버</option>
<option value="daum.net">다음</option>
<option value="nate.com">네이트</option>
<option value="gmail.com">gmail</option>
</select></td>
</tr>
<tr>
<td>전화번호</td>
<td><input type=text size=4 maxlength=4 id="tel1" name="tel1"
value="<%=member.getTel1()%>">- <input type=text size=4
maxlength=4 id="tel2" name="tel2" value="<%=member.getTel2()%>">-
<input type=text size=4 maxlength=4 id="tel3" name="tel3"
value="<%=member.getTel3()%>"></td>
</tr>
<tr>
<td>핸드폰</td>
<td><select id="phone1" name="phone1">
<option value="">번호선택</option>
<option value="010" <%if (member.getPhone1().equals("010")) {%>
<%="selected"%> <%}%>>010</option>
<option value="011" <%if (member.getPhone1().equals("011")) {%>
<%="selected"%> <%}%>>011</option>
<option value="016" <%if (member.getPhone1().equals("016")) {%>
<%="selected"%> <%}%>>016</option>
<option value="018" <%if (member.getPhone1().equals("018")) {%>
<%="selected"%> <%}%>>018</option>
<option value="019" <%if (member.getPhone1().equals("019")) {%>
<%="selected"%> <%}%>>019</option>
</select>- <input type=text size=4 maxlength=4 id="phone2" name="phone2"
value="<%=member.getPhone2()%>">- <input type=text size=4
maxlength=4 id="phone3" name="phone3"
value="<%=member.getPhone3()%>"></td>
</tr>
<tr>
<td>우편번호</td>
<td><input type=text size=5 maxlength=5 id="post" name="post"
value="<%=member.getPost()%>"> <input type=button
value="우편검색" onClick="openDaumPostcode()"></td>
</tr>
<tr>
<td>주소</td>
<td><input type=text size=45 id="address" name="address"
value="<%=member.getAddress()%>"></td>
</tr>
<tr>
<td>성별</td>
<td><input type=radio id="male" name="gender" value="남자"
<%if (member.getGender().equals("남자")) {%> <%="checked"%> <%}%>>남자
<input type=radio id="female" name="gender" value="여자"
<%if (member.getGender().equals("여자")) {%> <%="checked"%> <%}%>>여자</td>
</tr>
<tr>
<td>취미</td>
<td><input type=checkbox id="h1" name="hobby" value="공부">공부
<input type=checkbox id="h2" name="hobby" value="게임">게임 <input
type=checkbox id="h3" name="hobby" value="등산">등산 <input
type=checkbox id="h4" name="hobby" value="낚시">낚시 <input
type=checkbox id="h5" name="hobby" value="쇼핑">쇼핑</td>
</tr>
<tr>
<td>자기소개</td>
<td><textarea rows=5 cols=50 id="intro" name="intro"
placeholder="자기소개를 100자 이내로 입력하세요"><%=member.getIntro()%>"></textarea>
</td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="회원수정">
<input type=reset value="취소"></td>
</tr>
</table>
</form>
</body>
</html>
update.jsp
<%@page import="member.MemberDTO"%>
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="member" class="member.MemberDTO" />
<jsp:setProperty property="*" name="member" />
<%
String[] hobby = request.getParameterValues("hobby");
// 취미를 하이픈(-)을 붙여서 저장: ex) 공부-게임-등산..
String h = "";
for (String h1 : hobby) {
h += h1 + "=";
}
member.setHobby(h);
MemberDAO dao = MemberDAO.getInstance();
//한 사람의 상세 정보 구하기
MemberDTO old = dao.getMember(member.getId());
// 비번 비교
if (old.getPasswd().equals(member.getPasswd())){ // 비번 일치시
int result = dao.update(member);
if(result == 1){
%>
<script>
alert("회원정보 수정 성공");
location.href="main.jsp";
</script>
<% }
} else {// 비번 불일치시
%>
<script>
alert("비밀번호 불일치");
history.go(-1);
</script>
<%
}
%>
deleteform.jsp
<%@page import="member.MemberDTO"%>
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = (String) session.getAttribute("id");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 삭제 폼</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<!-- 외부 자바스크립트 파일 불러오기 -->
<script src="member.js"></script>
</head>
<body>
<form method="post" action="delete.jsp">
<input type="hidden" name="id" value="<%=id%>">
<table border=1 width=450 align="center">
<caption>회원 삭제 폼</caption>
<td>비밀번호</td>
<td><input type=password id="passwd" name="passwd"></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="회원삭제">
<input type=reset value="취소"></td>
</tr>
</table>
</form>
</body>
</html>
delete.jsp
<%@page import="member.MemberDTO"%>
<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="member" class="member.MemberDTO" />
<jsp:setProperty property="*" name="member" />
<%
MemberDAO dao = MemberDAO.getInstance();
//한 사람의 상세 정보 구하기
MemberDTO old = dao.getMember(member.getId());
// 비번 비교
if (old.getPasswd().equals(member.getPasswd())){ // 비번 일치시
int result = dao.delete(member.getId()); // delete SQL문 싷ㄹ행
if(result == 1){
session.invalidate(); // 세션 삭제
%>
<script>
alert("회원 탈퇴 성공");
location.href="main.jsp";
</script>
<% }
} else {// 비번 불일치시
%>
<script>
alert("비밀번호 불일치");
history.go(-1);
</script>
<%
}
%>
memberDAO.java
// DAO(Data Access Object)
package member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MemberDAO {
// 싱글톤 : 객체 생성을 한번만 수행하는 것.
private static MemberDAO instance = new MemberDAO();
public static MemberDAO getInstance() { // 정적 메소드
return instance;
}
// 컨넥션풀에서 컨넥션을 구해오는 메소드
private Connection getConnection() throws Exception {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/orcl");
return ds.getConnection();
}
// ID 중복 검사
public int memberAuth(String id) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql = "select * from member where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery(); // SQL문 실행
if (rs.next()) { // 조건식을 만족하는 데이터를 구구해옴
result = 1;
} else {
result = -1; // 사용 가능한 ID
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return result;
}
// 회원 가입
public int insert(MemberDTO member) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection(); // 컨넥션풀에서 컨넥션을 구해온다.
String sql = "insert into member ";
sql += " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, member.getId());
pstmt.setString(2, member.getPasswd());
pstmt.setString(3, member.getName());
pstmt.setString(4, member.getJumin1());
pstmt.setString(5, member.getJumin2());
pstmt.setString(6, member.getMailid());
pstmt.setString(7, member.getDomain());
pstmt.setString(8, member.getTel1());
pstmt.setString(9, member.getTel2());
pstmt.setString(10, member.getTel3());
pstmt.setString(11, member.getPhone1());
pstmt.setString(12, member.getPhone2());
pstmt.setString(13, member.getPhone3());
pstmt.setString(14, member.getPost());
pstmt.setString(15, member.getAddress());
pstmt.setString(16, member.getGender());
pstmt.setString(17, member.getHobby());
pstmt.setString(18, member.getIntro());
result = pstmt.executeUpdate(); // SQL문 실행
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return result;
}
// 로그인(회원인증 처리)
public int memberCheck(MemberDTO member) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql = "select * from member where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, member.getId());
rs = pstmt.executeQuery(); // SQL문 실행
if (rs.next()) { // ID 일치
if (rs.getString("passwd").equals(member.getPasswd())) {
result = 1; // ID, 비번 일치 (회원 인증 성공)
} else {
result = -1; // 비번 불일치
}
} else {
result = -2; // ID 불일치
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return result;
}
// 회원 수정폼 : 한 사람의 상세정보 구하기
public MemberDTO getMember(String id) {
MemberDTO member = new MemberDTO();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql = "select * from member where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
// member.id = "test; 접근안됨 private 접근제어자
member.setId(rs.getString("id"));
member.setPasswd(rs.getString("passwd"));
member.setName(rs.getString("name"));
member.setJumin1(rs.getString("jumin1"));
member.setJumin2(rs.getString("jumin2"));
member.setMailid(rs.getString("mailid"));
member.setDomain(rs.getString("domain"));
member.setTel1(rs.getString("tel1"));
member.setTel2(rs.getString("tel2"));
member.setTel3(rs.getString("tel3"));
member.setPhone1(rs.getString("phone1"));
member.setPhone2(rs.getString("phone2"));
member.setPhone3(rs.getString("phone3"));
member.setPost(rs.getString("post"));
member.setAddress(rs.getString("address"));
member.setGender(rs.getString("gender"));
member.setHobby(rs.getString("hobby"));
member.setIntro(rs.getString("intro"));
member.setRegister(rs.getTimestamp("register"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return member;
}
// 회원정보 수정
public int update(MemberDTO member) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql = "update member set name=?, jumin1=?, jumin2=?, mailid=?,";
sql += "domain=?, tel1=?, tel2=?, tel3=?, phone1=?, phone2=?, phone3=?,";
sql += "post=?, address=?, gender=?, hobby=?, intro=? where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, member.getName());
pstmt.setString(2, member.getJumin1());
pstmt.setString(3, member.getJumin2());
pstmt.setString(4, member.getMailid());
pstmt.setString(5, member.getDomain());
pstmt.setString(6, member.getTel1());
pstmt.setString(7, member.getTel2());
pstmt.setString(8, member.getTel3());
pstmt.setString(9, member.getPhone1());
pstmt.setString(10, member.getPhone2());
pstmt.setString(11, member.getPhone3());
pstmt.setString(12, member.getPost());
pstmt.setString(13, member.getAddress());
pstmt.setString(14, member.getGender());
pstmt.setString(15, member.getHobby());
pstmt.setString(16, member.getIntro());
pstmt.setString(17, member.getId());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return result;
}
// 회원 탈퇴
public int delete(String id) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql="delete from member where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
result = pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return result;
}
}