[jsp] 회원가입 빈즈, DTO, DAO 객체 생성

seulki·2022년 10월 19일
0

jsp

목록 보기
37/51
post-custom-banner

🎈UserBean.java

package com.koit.web.beans;

public class UserBean {
	private String userid ;
	private String userpw ;
	private String username ;
	private String usergender;
	private String userphone ;
		
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUserpw() {
		return userpw;
	}
	public void setUserpw(String userpw) {
		this.userpw = userpw;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsergender() {
		return usergender;
	}
	public void setUsergender(String usergender) {
		this.usergender = usergender;
	}
	public String getUserphone() {
		return userphone;
	}
	public void setUserphone(String userphone) {
		this.userphone = userphone;
	}
}


🎈join_view.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="joinForm" method="post" action="join_db.jsp">
		<p id="text">사용할 수 있는 아이디입니다.</p>
		<P>
			<label>아이디<input type="text" name="userid"></label>
			<input type="button" value="중복확인" onclick="">
		</P>
		
		<p>
			<label>비밀번호 <input type="password" name="userpw"></label>
		</p>
		<p>
			<label>비밀번호확인 <input type="password" name="userpw_re"> </label>
		</p>
		<p>
			<label>이름 <input type="text" name="username"> </label>
		</p>
		<p>
			성별</br>
			<label>남자 <input type="radio" name="usergender" value="M" checked></label>
			<label>여자 <input type="radio" name="usergender" value="W" checked></label>
		</p>
		<p>
			<label>핸드폰 번호 <input type="text" name="userphone"></label>
		</p>
		<p>
			우편번호
		</p>
		<p>
			주소
		</p>
		<p>
			상세주소
		</p>
		<p>
			<input type="button" value="가입완료" onclick="sendit()">
		</p>
	</form>
</body>
<script type="text/javascript" src="user.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</html>



🎈 user.js -> 유효성검사

function sendit(){
	let frm = document.joinForm;
	let userid = frm.userid;
	let userpw = frm.userpw;
	let userpw_re = frm.userpw_re;
	let username = frm.username;
	let usergender = frm.usergender;
	let userphone = frm.userphone;
	
	if(!userid.value){
		alert("아이디를 입력하세요");
		userid.focus();
		return false;
	}
	
	if(userid.value.length < 5 || userid.value.length >= 16){
		alert("아이디는 5자 이상, 16자 미만으로 입력하세요");
		userid.focus();
		return false;
	}
	
	if(userpw.value == ""){
		alert("비밀번호를 입력하세요");
		userpw.focus();
		return false;
	}
		
	if(userpw.value.length < 8){
		alert("비밀번호는 8자 이상 입력해주세요");
		userpw.focus();
		return false;
	}
	
	if(userpw.value !=  userpw_re.value){
		alert("비밀번호를 다시 확인하세요");
		userpw_re.focus();
		return false;
	}
	
	if(username.value == ""){
		alert("이름을 입력하세요");
		username.focus();
		return false;
	}
	
//	$("input[name='usergender']:checked").val();
// 라디오박스 체크 값 가져오기

	if(!userphone.value){
		alert("핸드폰 번호를 입력하세요");
		userphone.focus();
		return false;
	}
	
	frm.submit();
}


🎈UserDAO.java

package com.koit.web.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.koit.web.beans.UserBean;

public class UserDAO {

	Connection conn;
	PreparedStatement pstm;
	ResultSet rs;
	
	public boolean join(UserBean user) {
		boolean result = false;
		String sql = "INSERT INTO TBL_USER values(?, ?, ?, ?, ?)";
		int count = 0;
		
		try {
			conn = DBConnection.getConnection();
			pstm = conn.prepareStatement(sql);
			
			pstm.setString(1, user.getUserid());
			pstm.setString(2, user.getUserpw());
			pstm.setString(3, user.getUsername());
			pstm.setString(4, user.getUsergender());
			pstm.setString(5, user.getUserphone());
			
			//데이터 조작어가 실행된 건수 int로 반환
			count = pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(count != 0) {
			System.out.println("회원가입 성공");
			result = true;
		}else {
			System.out.println("회원가입 실패");
			result = false;
		}
		return result;
	}


🎈join_db.jsp

<%@page import="com.koit.web.dao.UserDAO"%>
<%@ 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>
	<!-- 컨트롤러 -->
	<jsp:useBean id="user" class="com.koit.web.beans.UserBean"/>
	<jsp:setProperty property="*" name="user"/>
	
	<%
	UserDAO udao = new UserDAO();
	boolean result = udao.join(user);

	if(result = true){
	%>
		<script type="text/javascript">
			alert("회원가입 성공");
			location.href = 'login_view.jsp';
		</script>
	<%
	}else {
	%>
		<script type="text/javascript">
			alert("회원가입 실패");
			location.href = 'join_view.jsp';
		</script>
	<%
	}
	%>
</body>
</html>
  • UserBean 객체 생성
  • 빈즈 객체에 setter 로 데이터 저장
  • UserDAO 객체 생성 후 join() 호출
  • 회원가입 성공이라면 로그인 페이지로 이동!
  • 실패라면 회원가입페이지로 이동!
profile
웹 개발자 공부 중
post-custom-banner

0개의 댓글