[Servlet&JSP] Chap 7. 회원 탈퇴 구현(MVC2 패턴)

김승현·2021년 12월 14일
0
post-custom-banner

index.jsp

<%@page import="kr.or.iei.member.model.vo.Member"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
	#loginForm{
		border : 1px solid grey;
		width : 300px;
		height : 250px;
		margin: 0 auto;
	}
	*{
	box-sizing: border-box;
	
	}
</style>
</head>
<body>
<H3>메인 페이지 (기본)</H3>

<%
	Member m = (Member)session.getAttribute("member");
%>

<%if(m!=null){ %>
	[<a href="/member/memberCheck.do"><%=m.getUserName()%></a>] 님 환영합니다!! <a href="/member/logout.do">로그아웃</a><br>
	
	
	<a href="/member/memberCheck.do">마이 페이지</a><br>
	<a href="/views/file/fileUploadPage.jsp">파일 업로드 페이지</a><br>
	<a href="/file/fileList.do">파일 목록 페이지</a><br>

	
	<%if(m.getRoll().substring(0, 2).equals("AD")){	//관리자로 구분되어진 사용자라면 %>

		<a href="/admin/memberAllList.do">회원관리 페이지</a><br>
		<a href="/admin/boardAllList.do">게시판 관리 페이지</a><br><br><hr>
	
	<%}else{ %>
		<a href="/member/memberWithDraw.do" id="withDraw">회원 탈퇴</a><br><br><hr>
	<%} %>
	
	<script>
		document.getElementById('withDraw').onclick=function(){
			
			
			// 1. 탈퇴를 진행하시겠습니까?
			// 2. 탈퇴를 하시게 되면, 데이터는 절대 복구 불가능합니다.
			
			if(window.confirm("탈퇴를 진행하시겠습니까?") && window.confirm("탈퇴를 하시게 되면, 데이터는 절대 복구 불가능합니다."))
			{
				return true;
			}else
			{
				return false;
			}

			
			
		};
	
	</script>
	
	
	
<%}else{ %>
<div id="loginForm">
	<form action="/member/login.do" method="post">
		<fieldset>
		<legend style="text-align: center;">로그인</legend>
		<input type="text"  class="b-block w-100" name="userId" placeholder="ID를 입력하세요"/><br><br>
		<input type="password" class="b-block w-100"  name="userPwd" placeholder="PW를 입력하세요"/><br><br>
		<input type="submit" class="btn btn-primary b-block w-100" value="로그인"/>
		</fieldset>
		<br>
		<a href="/views/member/memberJoinus.jsp" style="display:block; text-align: right;">회원가입</a>
	</form>
</div>

<%} %>

<a href="/board/boardAllSelectNP.do">게시판(페이징 처리 X)</a><br>
<a href="/board/boardAllSelect.do">게시판(페이징 처리 O)</a><br>
 
</body>
</html>



MemberWithDrawServlet.java

package kr.or.iei.member.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kr.or.iei.member.model.service.MemberService;
import kr.or.iei.member.model.service.MemberServiceImpl;
import kr.or.iei.member.model.vo.Member;

/**
 * Servlet implementation class MemberWithDrawServlet
 */
@WebServlet("/member/memberWithDraw.do")
public class MemberWithDrawServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberWithDrawServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 탈퇴하려는 회원을 구분할 수 있는 유니크 정보 추출
		HttpSession session=request.getSession();
		Member m = (Member)session.getAttribute("member");
		int userNo= m.getUserNo();
		
		// 비즈니스 로직 처리
		MemberService mService = new MemberServiceImpl();
		int result = mService.deleteOneMemeber(userNo);
		
		RequestDispatcher view= request.getRequestDispatcher("/views/member/memberWithDraw.jsp");
		
		if(result>0) {
			request.setAttribute("result", true);
			session.invalidate();// 회원 탈퇴를 했다면 세션 파기
		}else {
			request.setAttribute("result", false);
		}
		request.setAttribute("userId", m.getUserId());
		
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}



MemberService.java

package kr.or.iei.member.model.service;

import kr.or.iei.member.model.vo.Member;

public interface MemberService {
	
	public int deleteOneMemeber(int userNo);	
    
}



MemberServiceImpl.java

package kr.or.iei.member.model.service;

import java.sql.Connection;

import kr.or.iei.common.JDBCTemplate;
import kr.or.iei.member.model.dao.MemberDAO;
import kr.or.iei.member.model.vo.Member;
import sun.management.jdp.JdpBroadcaster;

public class MemberServiceImpl implements MemberService {

	private MemberDAO mDAO = new MemberDAO();

	@Override
	public int deleteOneMemeber(int userNo) {
		Connection conn = JDBCTemplate.getConnection();
		int result = mDAO.deleteOneMemeber(userNo, conn);

		if (result > 0) {
			JDBCTemplate.commit(conn);
		} else {
			JDBCTemplate.rollback(conn);
		}

		JDBCTemplate.close(conn);

		return result;
	}	


}



MemberDAO

package kr.or.iei.member.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import kr.or.iei.common.JDBCTemplate;
import kr.or.iei.member.model.vo.Member;

public class MemberDAO {

	public int deleteOneMemeber(int userNo, Connection conn) {
		PreparedStatement pstmt= null;
		int result = 0;
		
		String sql= "UPDATE MEMBER SET END_YN='Y' WHERE USERNO=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, userNo);
			
			result=pstmt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCTemplate.close(pstmt);
		}
		return result;
	}	
				
}



memberWithDraw.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%
		boolean result =(boolean)request.getAttribute("result");
		String userId = (String)request.getAttribute("userId");
	%>
	
	<script>
	
	<%if(result){ %>
		alert('[<%=userId%>] 회원 탈퇴 성공');
	<%}else{ %>
		alert('회원 탈퇴를 실패였습니다. \n- 지속적인 문제 발생시 관리자에게 문의해 주세여 -');
	<%} %>
	
	location.replace('/');
	
	</script>
	
</body>
</html>
profile
개발자로 매일 한 걸음
post-custom-banner

0개의 댓글