📝 회원 탈퇴 기능
💡 VS Code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<section class="left-side">
사이드 메뉴
<ul class="list-group">
<li> <a href="#">프로필</a> </li>
<li> <a href="${contextPath}/member/myPage/info">내 정보</a> </li>
<li> <a href="${contextPath}/member/myPage/changePw">비밀번호 변경</a> </li>
<li> <a href="${contextPath}/member/myPage/secession">회원 탈퇴</a> </li>
</ul>
</section>
🔎 myPage-secession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- 문자열 관련 함수(메소드) 제공 JSTL (EL 형식으로 작성) --%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Page</title>
<link rel="stylesheet" href="${contextPath}/resources/css/main-style.css">
<link rel="stylesheet" href="${contextPath}/resources/css/myPage-style.css">
<script src="https://kit.fontawesome.com/4dca1921b4.js" crossorigin="anonymous"></script>
</head>
<body>
<main>
<jsp:include page="/WEB-INF/views/common/header.jsp"/>
<section class="myPage-content">
<jsp:include page="/WEB-INF/views/member/sideMenu.jsp"/>
<section class="myPage-main">
<h1 class="myPage-title">회원 탈퇴</h1>
<span class="myPage-explanation">현재 비밀번호가 일치하는 경우 탈퇴할 수 있습니다.</span>
<form action="secession" method="POST" name="myPage-form">
<div class="myPage-row">
<label>비밀번호</label>
<input type="password" name="memberPw" maxlength="30">
</div>
<div class="myPage-row info-title">
<label>회원 탈퇴 약관</label>
</div>
<pre id="secession-terms">
제1조
이 약관은 샘플 약관입니다.
① 약관 내용 1
② 약관 내용 2
③ 약관 내용 3
④ 약관 내용 4
제2조
이 약관은 샘플 약관입니다.
① 약관 내용 1
② 약관 내용 2
③ 약관 내용 3
④ 약관 내용 4
</pre>
<div>
<input type="checkbox" name="agree" id="agree">
<label for="agree">위 약관에 동의합니다.</label>
</div>
<button id="info-update-btn">탈퇴</button>
</form>
</section>
</section>
</main>
<jsp:include page="/WEB-INF/views/common/footer.jsp"/>
</body>
</html>
🔎 myPage-style.css
.
.
.
#secession-terms{
width: 500px;
height: 300px;
border: 1px solid black;
font-size: 16px;
overflow: auto;
}
💡 Eclipse
🔎 MyPageSecessionServlet.java
package edu.kh.community.member.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import edu.kh.community.member.model.service.MemberService;
import edu.kh.community.member.model.vo.Member;
@WebServlet("/member/myPage/secession")
public class MyPageSecessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = "/WEB-INF/views/member/myPage-secession.jsp";
req.getRequestDispatcher(path).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String memberPw = req.getParameter("memberPw");
HttpSession session = req.getSession();
Member loginMember = (Member)( session.getAttribute("loginMember") );
int memberNo = loginMember.getMemberNo();
try {
MemberService service = new MemberService();
int result = service.secession(memberNo, memberPw);
String path = null;
if(result > 0) {
session.invalidate();
session = req.getSession();
session.setAttribute("message", "탈퇴되었습니다.");
path = req.getContextPath();
Cookie c = new Cookie("saveId", "");
c.setMaxAge(0);
c.setPath(req.getContextPath());
resp.addCookie(c);
} else {
session.setAttribute("message", "비밀번호가 일치하지 않습니다.");
path = "secession";
}
resp.sendRedirect(path);
} catch(Exception e) {
e.printStackTrace();
}
}
}
🔎 MemberService.java
public int secession(int memberNo, String memberPw) throws Exception {
Connection conn = getConnection();
int result = dao.secession(conn, memberNo, memberPw);
if(result > 0) commit(conn);
else rollback(conn);
close(conn);
return result;
}
🔎 MemberDAO.java
public int secession(Connection conn, int memberNo, String memberPw) throws Exception{
int result = 0;
try {
String sql = prop.getProperty("secession");
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, memberNo);
pstmt.setString(2, memberPw);
result = pstmt.executeUpdate();
} finally {
close(pstmt);
}
return result;
}