본문 바로가기
수업(국비지원)/JSP

[JSP] MVC MODEL1 방식 - 비밀번호 조회, 비밀번호 조회 구현(pwForm, pw)

by byeolsub 2023. 4. 19.

📌 pwForm.jsp 비밀번호 조회

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- /jsp2/src/main/webapp/model1/member/pwForm.jsp 
   pw.jsp 구현 : 파라미터값에 해당하는 비밀번호를 db에서 읽기
                화면에 비밀번호 출력.
--%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>비밀번호 찾기</title>
<link rel="stylesheet" href="../../css/main.css">
</head>
<body>
<h3>비밀번호 찾기</h3>
<form action="pw.jsp" method="post">
<table>
<tr><th>아이디</th><td><input type="text" name="id"></td></tr>
<tr><th>이메일</th><td><input type="text" name="email"></td></tr>
<tr><th>전화번호</th><td><input type="text" name="tel"></td></tr>
<tr><td colspan="2"><input type="submit" value="비밀번호 찾기"></td></tr>
</table>
</form>
</body>
</html>

 

📌 pw.jsp 비밀번호 조회 구현

<%-- /jsp2/src/main/webapp/model1/member/pw.jsp 
   1.파라미터값(id, email, tel) 저장
   2.파라미터값(id, email, tel) 에 해당하는 비밀번호를 db에서 읽기
   3.화면에 비밀번호 출력.
--%> 

 

<%@page import="model.MemberDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%  
//1.파라미터 값 구현
String id = request.getParameter("id");
String email = request.getParameter("email");
String tel = request.getParameter("tel");
//2.db에서 정보 읽어 비밀번호 값 리턴
String pass = new MemberDao().pwSearch(id, email, tel);
if(pass != null) { //비밀번호 존재.
%>   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>비밀번호 찾기</title>
<link rel="stylesheet" href="../../css/main.css">
</head>
<body>
<table>
<tr><th>비밀번호</th>
<td><%=pass %></td></tr>
<tr><td colspan="2"><input type="button" value="닫기" onclick="self.close()"></td></tr>
</table>
</body>
</html>
<% } else { //비밀번호 없음. %>
<script type="text/javascript">
   alert("정보에 맞는 pass를 찾을 수 없습니다.")
   location.href="pwForm.jsp"
</script>
<% } %>

 

 

📌 MemberDao.java 내용 추가

//비밀번호 찾기
	public String pwSearch(String id, String email, String tel) {
		Connection conn = DBConnection.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement //,인식 안됨. and로 나열 할 것
           ("select pass from member where id=? and email=? and tel=?");
			pstmt.setString(1, id);
			pstmt.setString(2, email);
			pstmt.setString(3, tel);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return rs.getString("pass");
			}
		} catch(SQLException e) {
			e.printStackTrace();
		}  finally {
			DBConnection.close(conn, pstmt, null);
		}
		return null;
		}
	}