일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- servlet
- 홈가든
- nppFTP
- 언어
- JSP
- configration
- 시스코
- vmware
- centos7
- packet-tracer
- comand
- board
- eclipse
- 설치
- interface
- 서버
- cisco
- CLI
- 무농약
- gcloud.gabia
- tomcat
- 이클립스
- war
- 게시판
- 패킷트레이서
- centos
- 한글
- java
- server
- 설정
- Today
- Total
넓고얕은지식사전
JSP & Servlet 게시판 구현하기(암호 변경) 본문
지난번에 로그아웃 기능을 구현 하였다.
https://nullgoyatten.tistory.com/20
그렇다면 이번에는 암호 변경 기능을 이어서 구현해보자.
1. Member 클래스 코드 추가
추가할 코드 :
public void changePassword(String newPwd) {
this.password = newPwd;
}
추가된 전체 코드 :
package member.model;
import java.util.Date;
public class Member {
private String id;
private String name;
private String password;
private Date regDate;
public Member(String id, String name, String password, Date regDate) {
this.id = id;
this.name = name;
this.password = password;
this.regDate = regDate;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public Date getRegDate() {
return regDate;
}
public boolean matchPassword(String pwd) {
return password.equals(pwd);
}
public void changePassword(String newPwd) {
this.password = newPwd;
}
}
2. MemberDao 클래스 코드 추가
추가할 코드 :
public void update(Connection conn, Member member) throws SQLException {
try (PreparedStatement pstmt = conn.prepareStatement(
"update member set name = ?, password = ? where memberid = ?")) {
pstmt.setString(1, member.getName());
pstmt.setString(2, member.getPassword());
pstmt.setString(3, member.getId());
pstmt.executeUpdate();
}
}
추가된 전체 코드 :
package member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import jdbc.JdbcUtil;
import member.model.Member;
public class MemberDao {
public Member selectById(Connection conn, String id) throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(
"select * from member where memberid = ?");
pstmt.setString(1, id);
rs = pstmt.executeQuery();
Member member = null;
if (rs.next()) {
member = new Member(
rs.getString("memberid"),
rs.getString("name"),
rs.getString("password"),
toDate(rs.getTimestamp("regdate")));
}
return member;
} finally {
JdbcUtil.close(rs);
JdbcUtil.close(pstmt);
}
}
private Date toDate(Timestamp date) {
return date == null ? null : new Date(date.getTime());
}
public void insert(Connection conn, Member mem) throws SQLException {
try (PreparedStatement pstmt =
conn.prepareStatement("insert into member values(?,?,?,?)")) {
pstmt.setString(1, mem.getId());
pstmt.setString(2, mem.getName());
pstmt.setString(3, mem.getPassword());
pstmt.setTimestamp(4, new Timestamp(mem.getRegDate().getTime()));
pstmt.executeUpdate();
}
}
public void update(Connection conn, Member member) throws SQLException {
try (PreparedStatement pstmt = conn.prepareStatement(
"update member set name = ?, password = ? where memberid = ?")) {
pstmt.setString(1, member.getName());
pstmt.setString(2, member.getPassword());
pstmt.setString(3, member.getId());
pstmt.executeUpdate();
}
}
}
3. MemberNotFoundException 작성 (암호를 변경할 회원 데이터가 존재하지 않는 경우)
경로 : member.service > MemberNotFoundException.java
package member.service;
public class MemberNotFoundException extends RuntimeException {
}
4. InvalidPasswordException 작성 (현재 암호가 일치하지 않는 경우)
경로 : member.service > InvalidPasswordException.java
package member.service;
public class InvalidPasswordException extends RuntimeException {
}
5. ChangePasswordService 작성
경로 : member.service > ChangePasswordService.java
package member.service;
import java.sql.Connection;
import java.sql.SQLException;
import jdbc.JdbcUtil;
import jdbc.connection.ConnectionProvider;
import member.dao.MemberDao;
import member.model.Member;
public class ChangePasswordService {
private MemberDao memberDao = new MemberDao();
public void changePassword(String userId, String curPwd, String newPwd) {
Connection conn = null;
try {
conn = ConnectionProvider.getConnection();
conn.setAutoCommit(false);
Member member = memberDao.selectById(conn, userId);
if (member == null) {
throw new MemberNotFoundException();
}
if (!member.matchPassword(curPwd)) {
throw new InvalidPasswordException();
}
member.changePassword(newPwd);
memberDao.update(conn, member);
conn.commit();
} catch (SQLException e) {
JdbcUtil.rollback(conn);
throw new RuntimeException(e);
} finally {
JdbcUtil.close(conn);
}
}
}
6. ChangePasswordHandler 작성
경로 : member.command > ChangePasswordHandler.java
package member.command;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import auth.service.User;
import member.service.ChangePasswordService;
import member.service.InvalidPasswordException;
import member.service.MemberNotFoundException;
import mvc.command.CommandHandler;
public class ChangePasswordHandler implements CommandHandler {
private static final String FORM_VIEW = "/WEB-INF/view/changePwdForm.jsp";
private ChangePasswordService changePwdSvc = new ChangePasswordService();
@Override
public String process(HttpServletRequest req, HttpServletResponse res)
throws Exception {
if (req.getMethod().equalsIgnoreCase("GET")) {
return processForm(req, res);
} else if (req.getMethod().equalsIgnoreCase("POST")) {
return processSubmit(req, res);
} else {
res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return null;
}
}
private String processForm(HttpServletRequest req, HttpServletResponse res) {
return FORM_VIEW;
}
private String processSubmit(HttpServletRequest req, HttpServletResponse res)
throws Exception {
User user = (User)req.getSession().getAttribute("authUser");
Map<String, Boolean> errors = new HashMap<>();
req.setAttribute("errors", errors);
String curPwd = req.getParameter("curPwd");
String newPwd = req.getParameter("newPwd");
if (curPwd == null || curPwd.isEmpty()) {
errors.put("curPwd", Boolean.TRUE);
}
if (curPwd == null || curPwd.isEmpty()) {
errors.put("newPwd", Boolean.TRUE);
}
if (!errors.isEmpty()) {
return FORM_VIEW;
}
try {
changePwdSvc.changePassword(user.getId(), curPwd, newPwd);
return "/WEB-INF/view/changePwdSuccess.jsp";
} catch (InvalidPasswordException e) {
errors.put("badCurPwd", Boolean.TRUE);
return FORM_VIEW;
} catch (MemberNotFoundException e) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
}
}
7. commandHandlerURI.properties 수정. ChangePasswordHandler가 /changePwd.do 요청을 처리하도록 매핑 설정에 추가한다.
/join.do=member.command.JoinHandler
/login.do=auth.command.LoginHandler
/logout.do=auth.command.LogoutHandler
/changePwd.do=member.command.ChangePasswordHandler
8. changePwdForm.jsp 작성
경로 : WebContent > WEB-INF > view > changePwdForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>암호 변경</title>
</head>
<body>
<form action="changePwd.do" method="post">
<p>
현재 암호:<br/><input type="password" name="curPwd">
<c:if test="${errors.curPwd}">현재 암호를 입력하세요.</c:if>
<c:if test="${errors.badCurPwd}">현재 암호가 일치하지 않습니다.</c:if>
</p>
<p>
새 암호:<br/><input type="password" name="newPwd">
<c:if test="${errors.newPwd}">새 암호를 입력하세요.</c:if>
</p>
<input type="submit" value="암호 변경">
</form>
</body>
</html>
9. changePwdSuccess.jsp 작성
경로 : WebContent > WEB-INF > view > changePwdSuccess.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>
<title>암호 변경</title>
</head>
<body>
암호를 변경했습니다.
</body>
</html>
10. 테스트
잘된다.
다음글 : 게시글 쓰기 구현
https://nullgoyatten.tistory.com/22
'IT > JSP & Servlet' 카테고리의 다른 글
JSP & Servlet 게시판 구현하기(게시글 목록 조회) (0) | 2023.02.06 |
---|---|
JSP & Servlet 게시판 구현하기(게시글 쓰기) (0) | 2023.01.14 |
JSP & Servlet 게시판 구현하기(로그아웃) (0) | 2023.01.13 |
JSP & Servlet 게시판 구현하기(로그인) (0) | 2023.01.12 |
JSP & Servlet 게시판 구현하기(가입) (0) | 2022.12.27 |