📝 비밀번호 수정 기능
💡 Eclipse
<%@ 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="#">회원 탈퇴</a> </li>
</ul>
</section>
🔎 myPage-info.jsp
.
.
.
<body>
<main>
<jsp:include page="/WEB-INF/views/common/header.jsp"/>
<section class="myPage-content">
<jsp:include page="/WEB-INF/views/member/sideMenu.jsp"/>
.
.
.
🔎 MyPageChangePwServlet.java
package edu.kh.community.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 edu.kh.community.member.model.service.MemberService;
import edu.kh.community.member.model.vo.Member;
@WebServlet("/member/myPage/changePw")
public class MyPageChangePwServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = "/WEB-INF/views/member/myPage-changePw.jsp";
req.getRequestDispatcher(path).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String currentPw = req.getParameter("currentPw");
String newPw = req.getParameter("newPw");
HttpSession session = req.getSession();
Member loginMember = (Member)(session.getAttribute("loginMember"));
int memberNo = loginMember.getMemberNo();
System.out.println(currentPw);
System.out.println(newPw);
System.out.println(memberNo);
try {
MemberService service = new MemberService();
int result = service.changePw(currentPw, newPw, memberNo);
String path = null;
if(result > 0) {
session.setAttribute("message", "비밀번호 성공!");
path = "info";
} else {
session.setAttribute("message", "현재 비밀번호가 일치하지 않습니다.");
path = "changePw";
}
resp.sendRedirect(path);
} catch(Exception e) {
e.printStackTrace();
}
}
}
🔎 myPage-changePw.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="changePw" method="POST" name="myPage-form">
<div class="myPage-row">
<label>현재 비밀번호</label>
<input type="password" name="currentPw" id="currentPw" maxlength="30">
</div>
<div class="myPage-row">
<label>새 비밀번호</label>
<input type="password" name="newPw" maxlength="30">
</div>
<div class="myPage-row">
<label>새 비밀번호 확인</label>
<input type="password" name="newPwConfirm" maxlength="30">
</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
.myPage-content{
width: 1000px;
height: 800px;
margin: 50px auto;
display: flex;
}
.left-side{
width: 25%;
border-right: 2px solid #ddd;
}
.list-group{
width: 100%;
padding: 0 20px 0 0;
}
.list-group > li{
list-style: none;
height: 50px;
font-size: 18px;
}
.list-group > li > a{
text-decoration: none;
color: black;
height: 100%;
border-bottom: 2px solid #ddd;
display: flex;
justify-content: center;
align-items: center;
}
.list-group > li:hover{
background-color: #f2f2f2;
}
.myPage-main{
width: 75%;
padding: 0 50px;
}
.myPage-title{
margin: 0 0 10px 0;
font-size: 30px;
}
.myPage-explanation{
display: block;
font-size: 14px;
margin-bottom: 30px;
letter-spacing: -1px;
}
.myPage-row{
width: 500px;
height: 50px;
margin-top: 20px;
display: flex;
align-items: center;
border-bottom: 2px solid #ddd;
}
.myPage-row > *{
font-size: 18px;
font-weight: bold;
}
.myPage-row > label{
width: 30%;
color: #455ba8;
}
.myPage-row > span{
width: 70%;
}
.myPage-row > input{
width: 100%;
height: 100%;
border: 0;
outline: 0;
font-weight: normal;
}
.myPage-row:focus-within{
border-bottom: 2px solid #455ba8;
}
form[name="myPage-form"]{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.info-title{
color: #455ba8;
border: 0;
}
.info-address{ margin: 0; }
#info-address-btn{
width: 30%;
height: 70%;
font-size: 14px;
font-weight: normal;
background-color: white;
border: 1px solid gray;
cursor: pointer;
}
#info-update-btn{
width: 100%;
padding: 10px;
margin: 50px 0;
border: none;
font-size: 20px;
font-weight: bold;
background-color: #455ba8;
color: white;
cursor: pointer;
}
.myPage-row > input[type="password"]{
width: 70%;
}
🔎EncryptWrapper.java
.
.
.
@Override
public String getParameter(String name) {
String value = null;
switch(name) {
case "inputPw" :
case "memberPw" :
case "currentPw" :
case "newPw" :
value = getSha512( super.getParameter(name) );
break;
default : value = super.getParameter(name);
}
return value;
}
.
.
.
🔎 MemberService.java
.
.
.
public int changePw(String currentPw, String newPw, int memberNo) throws Exception{
Connection conn = getConnection();
int result = dao.changePw(conn, currentPw, newPw, memberNo);
if(result > 0) commit(conn);
else rollback(conn);
close(conn);
return result;
}
.
.
.
🔎 MemberDAO.java
.
.
.
public int changePw(Connection conn, String currentPw, String newPw, int memberNo) throws Exception {
int result = 0;
try {
String sql = prop.getProperty("changePw");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, newPw);
pstmt.setInt(2, memberNo);
pstmt.setString(3, currentPw);
result = pstmt.executeUpdate();
} finally {
close(pstmt);
}
return result;
}
.
.
.