본문 바로가기
개발자/백엔드 웹 개발자 과정(국비)

[JSP]회원 가입, 아이디 중복 체크 실습

by 초응 2023. 1. 11.

memberDAO

package com.yong.member;

import java.sql.*;	
import java.util.*;

public class MemberDAO {

	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;

	public MemberDAO() {
		// TODO Auto-generated constructor stub
	}z

	/** db 연동 관련 메서드 */
	public void dbConnect() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String user = "scott";
			String pwd = "1234";
			conn = DriverManager.getConnection(url, user, pwd);

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/** 등록 관련 메서드 */
	public int memberJoin(MemberDTO dto) {
		try {
			dbConnect();
			String sql = "insert into jsp_member values(jsp_member_idx.nextval,?,?,?,?,?,?,sysdate)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, dto.getId());
			ps.setString(2, dto.getName());
			ps.setString(3, dto.getPwd());
			ps.setString(4, dto.getEmail());
			ps.setString(5, dto.getTel());
			ps.setString(6, dto.getAddr());

			int count = ps.executeUpdate();
			return count;

		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (Exception e2) {

			}
		}

	}

	/** 회원아이디 중복 검사 */
	public boolean idCheck(String userid) {
		try {
			dbConnect();
			String sql = "select id from jsp_member where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, userid);

			rs = ps.executeQuery();

			return rs.next();

		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (rs != null) rs.close();
				if (ps != null) ps.close();
				if (conn != null) conn.close();

			} catch (Exception e2) {

			}
		}
	}

}

memberDTO

package com.yong.member;
import java.sql.*;
public class MemberDTO {
	private int idx;
	private String id;
	private String name;
	private String pwd;
	private String email;
	private String tel;
	private String addr;
	private Date joindate;


	public MemberDTO() {
		super();
	}

	public MemberDTO(int idx, String id, String name, String pwd, String email, String tel, String addr,
			Date joindate) {
		super();
		this.idx = idx;
		this.id = id;
		this.name = name;
		this.pwd = pwd;
		this.email = email;
		this.tel = tel;
		this.addr = addr;
		this.joindate = joindate;
	}

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public Date getJoindate() {
		return joindate;
	}

	public void setJoindate(Date joindate) {
		this.joindate = joindate;
	}
	
	

}

 

memberJoin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>


	
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="/myweb/css/mainLayout.css">
<style>
h2 {
	text-align: center;
}

fieldset {
	width: 450px;
	margin: 0px auto;
}

fieldset table {
	margin: 0px auto;
}

h3 {
	text-align: center;
}
</style>
<script>
function open_idcheck(){
window.open('idCheck.jsp','idCheck' ,'width =420, height=200');
}
</script>
</head>
<body>
	<%@include file="/header.jsp"%>
	<section>
		<article>
			<h2>회 원 가 입</h2>
			<form name="join" action="memberJoin_ok.jsp">
				<fieldset>
					<legend>기본 가입 정보</legend>
					<table>
						<tr>
							<th>아이디</th>
							<td><input type="text" name="id" readonly onclick ="open_idcheck()">
						
							<td><input type="button" value="중복 검사" onclick ="open_idcheck()"></td>
						</tr>
						<tr>
							<th>이름</th>
							<td><input type="text" name="name"></td>
						</tr>
						<tr>
							<th>비밀번호</th>
							<td><input type="password" name="pwd"></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>
							<th>주소</th>
							<td><input type="text" name="addr"></td>
						</tr>
						<tr>
							<td colspan="3" align="center"><input type="submit"
								value="회원 가입"> <input type="reset" value="다시 작성"></td>
						</tr>
					</table>
				</fieldset>
			</form>
		</article>
	</section>
	<%@include file="/footer.jsp"%>

</body>
</html>

memberJoin_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<jsp:useBean id="mdto" class="com.yong.member.MemberDTO"></jsp:useBean>
<jsp:setProperty property="*" name="mdto" />
<jsp:useBean id="mdao" class="com.yong.member.MemberDAO"></jsp:useBean>
<%
int result = mdao.memberJoin(mdto);

String msg = result > 0 ? "회원가입을 축하합니다~!" : "회원가입에 실패하였습니다.";
%>

<script>
window.alert('<%=msg %>')
	window.location.href = '/myweb/index.jsp';
</script>

idCheck.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
<form name = "idCheck" action = "idCheck_ok.jsp">
<fieldset>
<legend>ID중복검사</legend>
<label>아이디:</label> 
<input type ="text" name ="userid"  required = "required"><input type ="submit" value ="중복검사">

</fieldset>

</form>
</body>
</html>

idCheck_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="mdao" class ="com.yong.member.MemberDAO"></jsp:useBean>

<%
String userid = request.getParameter("userid");
boolean result = mdao.idCheck(userid);

String msg = result?"사용할 수 없는 아이디입니다":"사용 가능한 아이디 입니다.";

if(result){
	%>
	<script>
	window.alert('<%=userid%>는 사용할 수 없는 아이디입니다');
	window.location.href = 'idCheck.jsp';
	</script>
	
	<%
}else{
	%>
	
	<script>
	window.alert('<%=userid%>는 사용 가능한 아이디 입니다.');
	opener.document.join.id.value ='<%=userid%>';
	window.self.close();
	</script>
<%
}
%>

'개발자 > 백엔드 웹 개발자 과정(국비)' 카테고리의 다른 글

[JSP] 쿠키(cookie)  (0) 2023.01.12
[JSP]DBCP 세팅  (0) 2023.01.12
[JSP]자바빈,Javabean,DTO,DAO  (0) 2023.01.09
[JSP]개발 순서, 웹jdbc연결,폼  (0) 2023.01.06
[JSP]directive(page,include), request,form  (0) 2023.01.05