index.jsp
memberJoinus.jsp
MemberJoinServlet.java(/member/memberJoin.do)
memberJoin.jsp
memberJoinus.jsp
idCheck.jsp
MemberIdCheckServlet.java (/member/idCheck.do)
idCheck.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>
<%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>
<%} %>
<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>
<%@ 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>회원 가입 Page</title>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<style>
fieldset{
width:350px;
height:500px;
margin:0 auto;
}
legend{
text-align:center;
}
</style>
</head>
<body>
<script>
function idCheck(){
window.open("/views/member/idCheck.jsp","_blank","width=300px height=100px");
}
</script>
<form action="/member/memberJoin.do" method="post">
<fieldset>
<legend>회원 가입</legend>
회원 ID : <input type="text" name="userId" id="userId" placeholder="ID를 입력하세요"> <input type="button" onclick="idCheck();" value="ID 중복확인"><br>
비밀 번호 : <input type="password" name="userPwd" placeholder="PW를 입력하세요"><br>
비밀 번호(re) : <input type="password" name="userPwd_re" placeholder="PW를 재입력하세요"><br>
이름 : <input type="text" name="userName" placeholder="이름을 입력하세요"><br>
나이 : <input type="text" name="age" size=3>세<br>
이메일 : <input type="email" name="email" size=30 placeholder="@ 형식의 이메일을 입력하세요"><br>
휴대폰 : <input type="text" name="phone" placeholder="폰번호를 입력하세요" maxlength=11><br>
주소 : <input type="text" id="sample5_address" name="address1" placeholder="주소를 입력하세요" onclick="sample5_execDaumPostcode()" ><br>
<input type="text" name="address2" placeholder="상세주소를 작성하세요" ><br>
성별 : <input type="radio" name="gender" value="M" checked>남
<input type="radio" name="gender" value="F">여<br>
취미 <br>
<input type="checkbox" name="hobby" value="운동">운동
<input type="checkbox" name="hobby" value="등산">등산
<input type="checkbox" name="hobby" value="독서">독서<br>
<input type="checkbox" name="hobby" value="노래">노래
<input type="checkbox" name="hobby" value="댄스">댄스
<input type="checkbox" name="hobby" value="기타">기타 <br>
<br><br>
<input type="submit" value="가입하기" id="joinBtn"> <input type="reset" value="다시 입력하기"><br><br>
<a href="/">메인 페이지로 돌아가기</a>
</fieldset>
</form>
<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
<script>
function sample5_execDaumPostcode() {
new daum.Postcode({
oncomplete: function(data) {
var addr = data.address; // 최종 주소 변수
// 주소 정보를 해당 필드에 넣는다.
document.getElementById("sample5_address").value = addr;
}
}).open();
}
</script>
<script>
$('#joinBtn').click(function(){
// 간단한 유효성 검사
var userPwd=$('input[name=userPwd]').val();
var userPwd_re=$('input[name=userPwd_re]').val();
if(userPwd!=userPwd_re){
alert('비밀번호와 비밀번호 재입력 값이 같아야 합니다.');
return false
}
return true;
});
</script>
</body>
</html>
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 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 MemberJoinServlet
*/
@WebServlet("/member/memberJoin.do")
public class MemberJoinServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MemberJoinServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 데이터 받기 전에 인코딩 처리
request.setCharacterEncoding("UTF-8");
// view Page에서 보내온 테이터 받기
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
String userName = request.getParameter("userName");
int age = Integer.parseInt(request.getParameter("age"));
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String address = request.getParameter("address1")+" "+ request.getParameter("address2");
char gender = request.getParameter("gender").charAt(0);
String[] hobbyValues = request.getParameterValues("hobby");
String hobby;
if (hobbyValues == null) {
hobby = "없음";
} else {
hobby = String.join(", ", hobbyValues);
}
Member m = new Member(userId, userPwd, userName, age, email, phone, address, gender, hobby);
// 비즈니스 로직 처리
MemberService mService = new MemberServiceImpl();
int result = mService.insertOneMember(m);
// 회원 가입 성공, 실패 처리(2가지 방법)
// 1. 회원 가입 성공하면 -> 성공 페이지로 이동 // 실패하면 -> 실패 페이지로 이동
// 2. 성공/실패 여부와 상관 없이 하나의 페이지로 이동(해당 페이지에서 성공/실패 처리)
// 방법 1.
/*if(result>0) { // 성공했다면
response.sendRedirect("/views/member/memberJoinSuccess.jsp");
}else { // 실패했다면
response.sendRedirect("/views/member/memberJoinFail.jsp");
}*/
// 방법 2.
RequestDispatcher view = request.getRequestDispatcher("/views/member/memberJoin.jsp");
if(result>0) {
request.setAttribute("result", true);
}else {
request.setAttribute("result", false);
}
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);
}
}
package kr.or.iei.member.model.service;
import kr.or.iei.member.model.vo.Member;
public interface MemberService {
public Member selectOneMember(String userId, String userPwd);
public int insertOneMember(Member m);
public int updateOneMember(Member m);
public int deleteOneMemeber(int userNo);
public Member refreshOneMember(int userNo);
public boolean selectIdCheck(String userId);
public int updatePwdMember(String userId, String pwd, String newPwd);
}
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 Member selectOneMember(String userId, String userPwd) {
Connection conn = JDBCTemplate.getConnection();
Member m = mDAO.selectOneMember(userId, userPwd, conn);
JDBCTemplate.close(conn);
return m;
}
@Override
public int insertOneMember(Member m) {
Connection conn = JDBCTemplate.getConnection();
int result = mDAO.insertOneMember(m, conn);
if (result > 0) {
JDBCTemplate.commit(conn);
} else {
JDBCTemplate.rollback(conn);
}
JDBCTemplate.close(conn);
return result;
}
@Override
public int updateOneMember(Member m) {
Connection conn = JDBCTemplate.getConnection();
int result = mDAO.updateOneMember(m, conn);
if(result>0) {
JDBCTemplate.commit(conn);
}else {
JDBCTemplate.rollback(conn);
}
JDBCTemplate.close(conn);
return result;
}
@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;
}
@Override
public Member refreshOneMember(int userNo) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean selectIdCheck(String userId) {
Connection conn = JDBCTemplate.getConnection();
boolean result = mDAO.selectIdCheck(userId, conn);
JDBCTemplate.close(conn);
return result;
}
@Override
public int updatePwdMember(String userId, String pwd, String newPwd) {
Connection conn= JDBCTemplate.getConnection();
int result = mDAO.updatePwdMember( userId, pwd, newPwd, conn);
if(result>0)JDBCTemplate.commit(conn);
else JDBCTemplate.rollback(conn);
JDBCTemplate.close(conn);
return result;
}
}
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 Member selectOneMember(String userId, String userPwd, Connection conn) {
PreparedStatement pstmt= null;
ResultSet rset = null;
Member m = null;
String sql="SELECT * FROM MEMBER WHERE userId=? AND userPwd=? AND end_YN='N'";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userId);
pstmt.setString(2, userPwd);
rset=pstmt.executeQuery();
if(rset.next()) {
m=new Member();
m.setUserNo(rset.getInt("userNo"));
m.setUserId(rset.getString("userId"));
m.setUserPwd(rset.getString("userPwd"));
m.setUserName(rset.getString("userName"));
m.setAge(rset.getInt("age"));
m.setEmail(rset.getString("email"));
m.setPhone(rset.getString("phone"));
m.setAddress(rset.getString("address"));
m.setGender(rset.getString("gender").charAt(0));
m.setHobby(rset.getString("hobby"));
m.setEnrollDate(rset.getDate("enrollDate"));
m.setEndYN(rset.getString("end_YN").charAt(0));
m.setRoll(rset.getString("roll"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(rset);
JDBCTemplate.close(pstmt);
}
return m;
}
public int insertOneMember(Member m, Connection conn) {
PreparedStatement pstmt= null;
int result=0;
String sql="INSERT INTO MEMBER VALUES(MEMBER_SEQ.NEXTVAL, ?,?,?,?,?,?,?,?,?,SYSDATE,'N',DEFAULT)";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, m.getUserId());
pstmt.setString(2, m.getUserPwd());
pstmt.setString(3, m.getUserName());
pstmt.setInt(4, m.getAge());
pstmt.setString(5, m.getEmail());
pstmt.setString(6, m.getPhone());
pstmt.setString(7, m.getAddress());
pstmt.setString(8, String.valueOf(m.getGender()));
pstmt.setString(9, m.getHobby());
result=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
public boolean selectIdCheck(String userId, Connection conn) {
PreparedStatement pstmt= null;
ResultSet rset=null;
boolean result=false;
String sql= "SELECT userId FROM MEMBER WHERE userId=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userId);
rset=pstmt.executeQuery();
if(rset.next()) {
result=true;
}else {
result=false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(rset);
JDBCTemplate.close(pstmt);
}
return result;
}
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;
}
public int updateOneMember(Member m, Connection conn) {
PreparedStatement pstmt= null;
int result = 0;
String sql="UPDATE MEMBER SET age=?,email=?, phone=?,address=?, gender=?, hobby=? WHERE userid=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, m.getAge());
pstmt.setString(2, m.getEmail());
pstmt.setString(3, m.getPhone());
pstmt.setString(4, m.getAddress());
pstmt.setString(5, String.valueOf(m.getGender()));
pstmt.setString(6, m.getHobby());
pstmt.setString(7, m.getUserId());
result=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
public int updatePwdMember(String userId, String pwd, String newPwd, Connection conn) {
PreparedStatement pstmt = null;
int result = 0;
String sql="UPDATE MEMBER SET userPwd=? WHERE userId=? AND userPwd=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, newPwd);
pstmt.setString(2, userId);
pstmt.setString(3, pwd);
result = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
}
<%@ 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");
%>
<%if(result){ %>
<h1>회원 가입을 축하합니다. Congratulation!</h1>
<a href="/">메인페이지로 이동</a>
<%}else{ %>
<script>
alert('회원가입에 실패하였습니다.\n지속적인 문제 발생시 \n관리자에게 문의해주세요');
location.replace('/');
</script>
<%} %>
</body>
</html>
<%@ 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>
<%
// result 값이 null 이거나 true/false
Object result = request.getAttribute("idCheck");
String userId =(String)request.getAttribute("userId");
%>
<%if(result==null){ %>
<form action="/member/idCheck.do" method="get">
<input type="text" name="userId" placeholder="중복 체크할 ID를 입력하세요">
<input type="submit" value="중복 체크">
</form>
<%}else{ %>
<form action="/member/idCheck.do" method="get">
<input type="text" name="userId" value="<%=userId %>" placeholder="중복 체크할 ID를 입력하세요">
<input type="submit" value="중복 체크">
</form>
<%if((boolean)result){ %>
<span style="color:red">해당 ID는 이미 사용 중합니다.</span>
<%}else{ %>
<span style="color:blue">해당 ID는 사용이 가능합니다.</span>
<button onclick="userId();">사용하기</button>
<script>
function userId(){
window.opener.document.getElementById('userId').value='<%=userId%>';
window.close();
}
</script>
<%} %>
<%} %>
</body>
</html>
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 kr.or.iei.member.model.service.MemberService;
import kr.or.iei.member.model.service.MemberServiceImpl;
/**
* Servlet implementation class MemberIdCheckServlet
*/
@WebServlet("/member/idCheck.do")
public class MemberIdCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MemberIdCheckServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 중복 체크를 확인할 userId 받기
String userId = request.getParameter("userId");
MemberService mService = new MemberServiceImpl();
boolean result = mService.selectIdCheck(userId);
RequestDispatcher view = request.getRequestDispatcher("/views/member/idCheck.jsp");
request.setAttribute("idCheck", result); // true 또는 false
request.setAttribute("userId", userId); // jsp 페이지에서 id값을 사용할 수 있도록 보내줌
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);
}
}