νμκ°μ
β’
κΈ°λ₯ μ€λͺ
β’
μμ
μμ
β’
μ€μ΅ μ½λ
β’
ν
μ€νΈ
β’
κ²°κ³Ό νλ©΄
(κ²°κ³Ό νλ©΄ μ¬μ§)
κΈ°λ₯ μ€λͺ
νμ κ°μ
κΈ°λ₯μ μ¬μ©μμ μ 보λ₯Ό ν΄λΉ μ¬μ΄νΈ(μλ²)μ μ μ₯νλ κΈ°λ₯μ
λλ€.
- μ¬μ©μλ νλ©΄μμ μ¬μ©μ μ 보(μμ΄λ, λΉλ°λ²νΈ λ±)μ μ
λ ₯ν©λλ€.
- μ¬μ©μ μ 보μ ν¨κ» μλ²μ κ°μ
μμ²μ 보λ
λλ€.
- μλ²μμλ μ¬μ©μμ μ 보λ₯Ό DB μ μ μ₯ ν, μλ΅ν©λλ€.
β’
νμ κ°μ
νλ©΄
β’
μ ν¨μ± κ²μ¬
β’
νμ κ°μ
μμ²
β’
νμ κ°μ
μ²λ¦¬
β’
νμ κ°μ
μ²λ¦¬ ν μλ΅
νμ κ°μ νλ©΄
μ¬μ©μκ° κ°μ
μ 보λ₯Ό μ
λ ₯νλ νλ©΄μ
λλ€.
μ ν¨μ± κ²μ¬
μ¬μ΄νΈμ μ μ±
μ λ°λΌ, μμ΄λ, λΉλ°λ²νΈ λ± μ ν¨ν κ°λ§ μ
λ ₯μ΄ κ°λ₯νλλ‘ κ²μ¬νλ κ³Όμ μ
λλ€.
β’
null 체ν¬
β’
μμ΄λ μ€λ³΅ κ²μ¬
β’
λΉλ°λ²νΈ 쑰건 κ²μ¬
β’
μ΄λ©μΌ νμ κ²μ¬
null 체ν¬
κ°μ μ
λ ₯νμ§ μμμ, null, ββ(λΉ λ¬Έμμ΄) λ±μ΄ μλ²λ‘ μμ²λμ§ μλλ‘ νμΈν©λλ€.
null, ββ(λΉ λ¬Έμμ΄) λ±μ΄ μλ²λ‘ μμ²λλ©΄, NullPointerException λ±μ΄ λ°μν μ μμ΅λλ€.
function checkNull(value) {
return (value === null || value.trim() === '');
}
JavaScript
볡μ¬
null λ° ββ(λΉ λ¬Έμμ΄) 체ν¬λ μμ£Ό μ¬μ©λλ 쑰건μΌλ‘ κ³΅ν΅ ν¨μλ‘ μ μνμ¬ μ¬μ¬μ©νλ κ²μ΄ μ’μ΅λλ€.
μμ΄λ μ€λ³΅ κ²μ¬
μμ΄λλ κ³ μ ν κ°μ
λλ€. DB μμλ κΈ°λ³Έν€(PK), κ³ μ ν€(UK) λ±μΌλ‘ μ§μ νκΈ° λλ¬Έμ, μ΄λ―Έ μλ μμ΄λκ° μμ²λλ€λ©΄ μλ² μΈ‘μμ μλ¬κ° λ°μν μ μμ΅λλ€.
μ€λ³΅λ μμ΄λλ₯Ό μμ²νκ² λλ©΄, λ°μ΄ν°λ² μ΄μ€μμ λ¬΄κ²°μ± μ μ½ μ‘°κ±΄μ μλ°°λ μ μμ΅λλ€.
(μμ΄λ μ€λ³΅ κ²μ¬ νλ©΄)
μμ΄λ μ€λ³΅ κ²μ¬λ λΉλκΈ° μ²λ¦¬λ₯Ό ν΅ν΄ ꡬνν μ μμ΅λλ€. μμ΄λ μ€λ³΅ κ²μ¬μ λνμ¬ λκΈ°μ form μμ²μ νκ² λλ©΄ νλ©΄ μ΄λμ νλ©΄μ μ
λ ₯ν μ λ³΄κ° μ¬λΌμ§κ±°λ μμ²μ μ¬λ¬ νλ©΄μ κ±Έμ³μ μ§νν΄μΌν©λλ€.
λ°λΌμ, μμ΄λ μ€λ³΅ κ²μ¬λ νλ©΄ μ΄λ μμ΄ λΉλκΈ° μμ²μ 보λ΄μ΄ μ€λ³΅ μ¬λΆμ λν μλ΅ λ°μ΄ν°λ₯Ό λ°μμ κ·Έ μ¬λΆλ₯Ό UI λ° κ²μ¬ μ¬λΆμ ν¬ν¨μν΅λλ€.
β’
μμ΄λ μ€λ³΅ κ²μ¬ λ²νΌ
<button type="button" onclick="clickIdCheck()" class="btn btn-success col-md-2">μ€λ³΅νμΈ</button>
HTML
볡μ¬
β’
μμ΄λ μ€λ³΅ κ²μ¬ λΉλκΈ° μ²λ¦¬
// μμ΄λ μ€λ³΅ νμΈ
function idCheck() {
let username = document.getElementById('username').value
let url = `${root}/users/idCheck`
let data = {
'username' : username,
}
return new Promise((resolve, reject) => {
// jQuery λ‘ AJAX μμ²
$.ajax({
type : 'GET', // μμ² λ©μλ
url : url, // μμ² URL
data : data, // μμ² λ°μ΄ν°
contentType : 'application/json', // μμ² λ°μ΄ν° νμ
dataType : 'html', // μλ΅ λ°μ΄ν° νμ
// μμ² μ±κ³΅
success : function(response, status) {
// response : μλ΅ λ°μ΄ν°
// status : μλ΅ μν
if( response == '' ) {
alert('μλ΅ λ©μμ§κ° μμ΅λλ€')
}
resolve(response == 'true' ? true : false)
},
// μλ¬
error : function(xhr, status) {
// xhr : XMLHttpRequest κ°μ²΄
// status : μλ΅ μν
alert('μλ¬ λ°μ')
reject('error')
}
})
})
}
JavaScript
볡μ¬
λΉλ°λ²νΈ 쑰건 κ²μ¬
λΉλ°λ²νΈλ μ¬μ΄νΈμ 보μ μ μ±
μ λ°λ₯Έ ν¨ν΄μ 맀νλλμ§ νμΈν©λλ€.
μλ₯Ό λ€λ©΄, 8 μ μ΄μμ μλ¬Έμ, μ«μ, νΉμ λ¬Έμλ₯Ό λͺ¨λ ν¬ν¨νλ μ§λ₯Ό κ²μ¬ν©λλ€.
μ΄λ κ² μ‘°κ±΄μ΄λ ν¨ν΄μ΄ 볡μ‘ν μ ν¨μ±μ κ²μ¬νλ κ²½μ°μλ μ κ· ννμμ μ¬μ©νλ κ²μ΄ μ’μ΅λλ€.
μ κ·ννμμ΄λ?
: νΉμ ν¨ν΄μ μ°Ύκ±°λ 맀μΉνκΈ° μν λ¬Έμμ΄μ μ§ν©μ μ μνλλ° μ¬μ©λλ νμ μΈμ΄
μ κ· ννμμ λνμ¬λ μλ νμ΄μ§μμ μ‘°κΈ λ μμΈν λ€λ£¨κ³ μμ΅λλ€.
β’
μ κ· ννμμ μ¬μ©νμ¬ λΉλ°λ²νΈ μ ν¨μ± κ²μ¬
// λΉλ°λ²νΈ μ ν¨μ± κ²μ¬
let passwordCheck = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
msg = 'λΉλ°λ²νΈλ μλ¬Έμ, μ«μ, νΉμλ¬Έμλ₯Ό ν¬ν¨νμ¬ 8μ μ΄μμΌλ‘ μ
λ ₯ν΄μ£ΌμΈμ.'
if (!check(passwordCheck, password, msg)) return false
JavaScript
볡μ¬
μ΄λ©μΌ νμ κ²μ¬
μ΄λ©μΌ νμμ΄ μ¬λ°λ₯Έμ§ μ κ· ννμμ μ΄μ©νμ¬ κ²μ¬ν©λλ€.
μ¬μ©μμ κ°μ
μ λ³΄λ‘ μ΄λ©μΌμ λ§μ΄ μ¬μ©νλλ°, λλ‘λ μ΄λ©μΌλ‘ μΈμ¦ λ²νΈ λλ λ§ν¬λ₯Ό λ°μ‘νμ¬ μ¬μ©μ μΈμ¦μ΄λ λΉλ°λ²νΈ μ°ΎκΈ° κΈ°λ₯ λ±κ³Ό μ°λνλ κ²½μ°κ° μμ΅λλ€.
μ΄ λ, μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μλ€λ©΄, μ΄λ©μΌμ΄ λ°μ‘λμ§ μμ μ μμ΅λλ€. λ°λΌμ μ΄λ©μΌ νμ ν¨ν΄μ΄ μ¬λ°λ₯Έμ§ κ²μ¬νμ¬ μ
λ ₯ λ°μ΅λλ€. μ΄λ©μΌ νμμ OOOO@OOOO.com μ΄μ κ°μ ν¨ν΄μΌλ‘ λ€μ 볡μ‘ν νμμ κ°μ§κ³ μμ΅λλ€.
μ΄λ©μΌ νμμ μ κ· ννμμ μ¬μ©νλ©΄ μ½κ² κ²μ¬ν μ μμ΅λλ€.
// μ΄λ©μΌ μ ν¨μ± κ²μ¬
let emailCheck = /^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i
msg = 'μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.'
if (!check(emailCheck, email, msg)) return false
JavaScript
볡μ¬
νμ κ°μ μμ²
μ
λ ₯ν λ°μ΄ν°κ° λͺ¨λ μ ν¨νμ§ κ²μ¬ ν, POST λ°©μμΌλ‘ form μμ²μ 보λ
λλ€.
μλ² μΈ‘μμ μ
λ ₯ν λ°μ΄ν°μ λν μ΄λ¦μ input νκ·Έμ name μμ± κ°μΌλ‘ μΈμν©λλ€.
GET λ°©μμΌλ‘ μ£Όμ λ°μ΄ν°λ₯Ό μμ²νκ² λλ©΄, URL μ λ
ΈμΆμ΄ λλ―λ‘ λ³΄μ μ μνν©λλ€.
name=βνλΌλ―Έν°λͺ
β κ³Ό request.getParameter(βνλΌλ―Έν°λͺ
β) μ΄ μΌμΉν΄μΌ μμ²ν κ°μ, μλ² μΈ‘μμ μ νν μ λ¬ λ°μ μ μμ΅λλ€.
β’
νμ κ°μ
μμ² form
<form action="join_pro.jsp" name="joinForm" method="post">
<input type="hidden" name="duplicaed" id="duplicaed" value="false" />
<div class="input-group mb-3 row">
<label for="username" class="input-group-text col-md-4" id="lb_username">μμ΄λ</label>
<input type="text" class="form-control col-md-6"
name="username" id="username" placeholder="μμ΄λ" required>
<button type="button" onclick="clickIdCheck()" class="btn btn-success col-md-2">μ€λ³΅νμΈ</button>
</div>
<div class="input-group mb-3 row">
<label for="password" class="input-group-text col-md-4" id="lb_password">λΉλ°λ²νΈ</label>
<input type="password" class="form-control col-md-8"
name="password" id="password" placeholder="λΉλ°λ²νΈ" required>
</div>
<div class="input-group mb-3 row">
<label for="password_confirm" class="input-group-text col-md-4" id="lb_password_confirm">λΉλ°λ²νΈ νμΈ</label>
<input type="password" class="form-control col-md-8"
name="password_confirm" id="password_confirm" placeholder="λΉλ°λ²νΈ νμΈ" required>
</div>
<div class="input-group mb-3 row">
<label for="name" class="input-group-text col-md-4" id="lb_name">μ΄λ¦</label>
<input type="text" class="form-control col-md-8"
name="name" id="name" placeholder="μ΄λ¦" required>
</div>
<div class="input-group mb-3 row">
<label for="email" class="input-group-text col-md-4" id="lb_email">μ΄λ©μΌ</label>
<input type="text" class="form-control col-md-8"
name="email" id="email" placeholder="μ΄λ©μΌ">
</div>
<div class="d-grid gap-2 mt-5 mb-5 d-md-flex justify-content-md-between">
<a href="javascript: history.back()" class="btn btn-lg btn-secondary">μ·¨μ</a>
<input type="button" class="btn btn-lg btn-primary" value="κ°μ
" onclick="checkUser()" />
</div>
</form>
HTML
볡μ¬
νμ κ°μ μ²λ¦¬
ν΄λΌμ΄μΈνΈ μΈ‘μμ μμ²ν μ¬μ©μ μ 보λ₯Ό μ λ¬ λ°κ³ , μ΄λ₯Ό λ°μ΄ν° μ μ‘ κ°μ²΄(DTO)μ λ΄μ νμ κ°μ
μ²λ¦¬ λ‘μ§μΌλ‘ μ λ¬ν©λλ€.
β’
Servlet or JSP
β’
DTO
β’
Service
β’
DAO
Servlet or JSP
νμ κ°μ
νλ©΄μμ, μμ²μ λ°μ μ²λ¦¬ν Servlet λλ JSP μ½λλ₯Ό μμ±ν©λλ€.
β’
join_pro.jsp
<%--
νμ κ°μ
μ²λ¦¬
--%>
<%@page import="users.service.UserService"%>
<%@page import="users.service.UserServiceImpl"%>
<%@page import="users.model.Users"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Users user = new Users();
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String email = request.getParameter("email");
user.setUsername(username);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
// νμ μ 보 λ±λ‘ μμ²
UserService userService = new UserServiceImpl();
int result = userService.join(user);
String root = request.getContextPath();
if( result > 0 ) {
// λ‘κ·ΈμΈ νλ©΄μΌλ‘ μ΄λ
response.sendRedirect(root + "/users/login.jsp");
} else {
// νμκ°μ
νμ΄μ§λ‘ μ΄λ
response.sendRedirect(root + "/users/join.jsp?error");
}
%>
HTML
볡μ¬
DTO
κ³μΈ΅ κ°μ, μ¬λ¬ λ°μ΄ν° λ΄μ μ£Όκ³ λ°μ κ°μ²΄λ₯Ό μ μν©λλ€.
β’
Users
package users.model;
import java.util.Date;
import lombok.Data;
@Data
public class Users {
private String id;
private String username;
private String password;
private String name;
private String email;
private Date regDate;
private Date updDate;
}
HTML
볡μ¬
Service
μ²λ¦¬ λ‘μ§(λΉμ¦λμ€ λ‘μ§)μ μμ±ν©λλ€.
- μΈν°νμ΄μ€μ ꡬν ν΄λμ€λ‘ μ μν©λλ€.
β’
UserService
β’
UserServiceImpl
β’
UserService
package users.service;
import users.model.Users;
public interface UserService {
// νμκ°μ
public int join(Users user);
// μμ΄λ μ€λ³΅ νμΈ
public boolean idCheck(String username);
}
Java
볡μ¬
β’
UserServiceImpl
package users.service;
import users.data.UserDAO;
import users.model.Users;
public class UserServiceImpl implements UserService {
private UserDAO userDAO = new UserDAO();
/**
* νμκ°μ
*/
@Override
public int join(Users user) {
int result = userDAO.join(user);
if( result > 0 ) {
System.out.println("νμ κ°μ
μ²λ¦¬ μ±κ³΅!");
} else {
System.out.println("νμ κ°μ
μ€ν¨!");
}
// κΈ°λ³Έ νμ κΆνμ μΆκ°...
return result;
}
/**
* μμ΄λ μ€λ³΅ νμΈ
*/
@Override
public boolean idCheck(String username) {
boolean result = userDAO.idCheck(username);
return result;
}
}
Java
볡μ¬
DAO
λ°μ΄ν° λ² μ΄μ€μ μ κ·Όνλ κ°μ²΄λ‘, μ¬μ©νλ DBMS μμ μ 곡νλ JDBC λλΌμ΄λ²λ₯Ό μ¬μ©ν©λλ€.
β’
UserDAO
package users.data;
import java.sql.SQLException;
import java.util.UUID;
import common.jdbc.JDBConnection;
import users.model.PersistentLogins;
import users.model.Users;
public class UserDAO extends JDBConnection {
/**
* νμ κ°μ
* @param user λ±λ‘ν νμ μ 보
* @return λ±λ‘λ λ°μ΄ν° κ°μ
*/
public int join(Users user) {
int result = 0;
String sql = "INSERT INTO users (id, username, password, name, email, reg_date, upd_date) " +
"VALUES (RAWTOHEX(SYS_GUID()), ?, ?, ?, ?, sysdate, sysdate)";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, user.getUsername());
psmt.setString(2, user.getPassword());
psmt.setString(3, user.getName());
psmt.setString(4, user.getEmail());
result = psmt.executeUpdate();
} catch (SQLException e) {
System.err.println("νμ κ°μ
μ€ μ€λ₯ λ°μ");
e.printStackTrace();
}
return result;
}
/**
* μμ΄λ μ€λ³΅ νμΈ
* @param username
* @return
*/
public boolean idCheck(String username) {
// SQL μμ±
String sql = " SELECT * "
+ " FROM users "
+ " WHERE username = ? ";
try {
psmt = con.prepareStatement(sql);
psmt.setString( 1, username );
rs = psmt.executeQuery();
if( rs.next() ) {
return true; // μμ΄λ μ€λ³΅λ¨
}
return false;
} catch(SQLException e) {
System.err.println("μμ΄λ μ€λ³΅ νμΈ μ, μμΈ λ°μ");
e.printStackTrace();
}
return false;
}
}
Java
볡μ¬
νμ κ°μ μ²λ¦¬ ν μλ΅
νμ κ°μ
μλ£ μ¬λΆμ λ°λΌμ, νμ΄μ§ μλ΅μ μ§νν©λλ€.
β’
νμ κ°μ
μ±κ³΅
β’
νμ κ°μ
μ€ν¨
νμ κ°μ μ±κ³΅
νμ κ°μ
μ±κ³΅ μ, νμ΄μ§λ₯Ό μ΄λμν΅λλ€.
νμ κ°μ
μ±κ³΅ ν, μ΄λν νμ΄μ§μ μΌμ΄μ€λ λ€μκ³Ό κ°μ΅λλ€.
κ°μ
μλ£ νλ©΄
λ‘κ·ΈμΈ νλ©΄
λ©μΈ νλ©΄
κ°μ
μλ£ νλ©΄
κ³Όκ±°μ UI/UX μμλ νμ κ°μ
ν λ€μ ν λ² νμ κ°μ
μ 보 λ° μ²λ¦¬ μ¬λΆμ λν΄μ μΆλ ₯νλ νλ©΄μΌλ‘ μ΄λνκ³€ νμ΅λλ€. κ·Έλ¬λ, κ°μ
μλ£ νλ©΄μΌλ‘ μ΄λνλλΌλ λλ€μμ μ¬μ©μλ€μ λ°λ‘ λ‘κ·ΈμΈμ μλνλ €κ³ ν κ²μ
λλ€.
λ°λΌμ, κ°μ
μ 보 λ° μλ£ μ¬λΆμ λνμ¬ λ°λμ λ€μ 리ν¬νΈν νμκ° μλ€λ©΄, λ‘κ·ΈμΈ νλ©΄ λλ λ©μΈ νλ©΄μΌλ‘ μ΄λνλ κ²μ΄ λ λμ μ¬μ©μ κ²½νμ μ 곡ν κ²μ
λλ€.
λ‘κ·ΈμΈ νλ©΄
νμ κ°μ
ν, λλΆλΆμ μ¬μ©μλ€μ λ°λ‘ λ‘κ·ΈμΈμ μλν κ²μ
λλ€. λ°λΌμ νμ κ°μ
μ΄ μλ£λμλ€λ©΄ λ‘κ·ΈμΈ νμ΄μ§λ‘ μ΄λμμΌ, μ¬μ©μκ° λ‘κ·ΈμΈμ ν μ μλλ‘ μ λν©λλ€.
λ©μΈ νλ©΄
μ¬μ©μλ λ‘κ·ΈμΈμ νκΈ° μν΄μ νμ κ°μ
μ μ§ννμμ κ²μ
λλ€. λ°λΌμ, μ¬μ©μμκ² νμ κ°μ
μ μ
λ ₯ν μμ΄λ λ° λΉλ°λ²νΈλ₯Ό λ μ
λ ₯νκ² νλ κ²μ λ€μ λ²κ±°λ‘μ΄ μΌμ΄ λ μ μμ΅λλ€. μ΄λ―Έ νμ κ°μ
μ μμ΄λ λ° λΉλ°λ²νΈλ₯Ό μ
λ ₯νμκΈ° λλ¬Έμ μλ² μΈ‘μμ λ‘κ·ΈμΈ μΈμ¦ μ²λ¦¬ ν λ©μΈ νλ©΄μ λ‘κ·ΈμΈ λ μνλ‘ μλ΅ν΄μ£Όλ κ²λ μ’μ λ°©λ²μ
λλ€,
νμ κ°μ
μ²λ¦¬ μ±κ³΅ λ°λ‘ λ‘κ·ΈμΈ λ©μΈ νλ©΄
λ°λ‘ λ‘κ·ΈμΈ κΈ°λ₯μ κ²½μ°, λ‘κ·ΈμΈ κΈ°λ₯μ ꡬν ν μ μ©ν΄μΌν©λλ€.
νμ κ°μ μ€ν¨
νμ κ°μ
μ€ν¨ μ, νμ κ°μ
μ 보λ₯Ό λ€μ μ
λ ₯νλλ‘ νμν λ©μμ§μ ν¨κ» νμ κ°μ
νμ΄μ§λ‘ λ€μ μ΄λν©λλ€.
νμ κ°μ
μ²λ¦¬ μ€ν¨ νμ κ°μ
νλ©΄
μμ μμ
1.
νμ μ 보 ERD μμ±
2.
users - νμ ν
μ΄λΈ μμ±
3.
common.jsp - κ³΅ν΅ μ μ© νμ΄μ§ μμ±
4.
join.jsp (νμκ°μ
νλ©΄) νμ΄μ§ μμ±
5.
μ ν¨μ± κ²μ¬ μ½λ μμ±
6.
JDBConnection.java - JDBC λλΌμ΄λ² μ°λ
7.
UserDAO.java - λ°μ΄ν° μ κ·Ό κ°μ²΄ μμ±
8.
UserService.java - μλΉμ€ μΈν°νμ΄μ€ μμ±
9.
UserServiceImpl.java - μλΉμ€ ꡬν ν΄λμ€ μμ±
10.
join_pro.jsp (νμ κ°μ
μ²λ¦¬) νμ΄μ§ μμ±
μ€μ΅ μ½λ
νμ μ 보 ERD μμ±
users - νμ ν μ΄λΈ μμ±
CREATE TABLE users (
id VARCHAR2(255) NOT NULL PRIMARY KEY,
username VARCHAR2(100) NULL,
password VARCHAR2(100) NULL,
name VARCHAR2(50) NULL,
email VARCHAR2(200) NULL,
reg_date DATE DEFAULT sysdate NULL,
upd_date DATE DEFAULT sysdate NULL
);
SQL
볡μ¬
common.jsp - κ³΅ν΅ μ μ© νμ΄μ§ μμ±
β’
common.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String root = request.getContextPath();
%>
<c:set var="root" value="<%= root %>" />
Java
볡μ¬
<%= request.getContextPath() %>
Java
볡μ¬
μ μ½λμ κ°μ΄, λ°λ³΅μ μΌλ‘ μ¬μ©λλ μ½λλ₯Ό κ³΅ν΅ λͺ¨λ (common.jsp) μ μ μ©ν΄λκ³ μ¬μ©νκ² μ΅λλ€.
μ΄λ κ² ContextPath λ₯Ό 미리 root λΌλ λ³μλͺ
μΌλ‘ μ μ₯ν΄λλ©΄, link νκ·Έ λλ script νκ·Έμμ ν΄λΉ νμΌμ κ²½λ‘λ₯Ό μ§μ ν λ μλμ κ°μ΄ λ¨μνκ² μ§μ ν μ μμ΅λλ€.
<jsp:include page="${ root }/layout/import/link.jsp" />
Java
볡μ¬
join.jsp (νμκ°μ νλ©΄) νμ΄μ§ μμ±
νμ κ°μ νλ©΄
β’
join.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- κ³΅ν΅ μ μ© -->
<jsp:include page="/layout/main/import/common.jsp" />
<jsp:include page="/layout/main/import/meta.jsp" />
<jsp:include page="/layout/main/import/link.jsp" />
</head>
<body>
<jsp:include page="/layout/main/UI/header.jsp" />
<div class="px-4 py-5 mt-5 text-center">
<h1 class="display-5 fw-bold text-body-emphasis">νμ κ°μ
</h1>
</div>
<!-- νμ κ°μ
μμ -->
<div class="container shop p-5 mb-5" >
<form action="join_pro.jsp" name="joinForm" method="post">
<input type="hidden" name="duplicaed" id="duplicaed" value="false" />
<div class="input-group mb-3 row">
<label for="username" class="input-group-text col-md-4" id="lb_username">μμ΄λ</label>
<input type="text" class="form-control col-md-6"
name="username" id="username" placeholder="μμ΄λ" required>
<button type="button" onclick="clickIdCheck()" class="btn btn-success col-md-2">μ€λ³΅νμΈ</button>
</div>
<div class="input-group mb-3 row">
<label for="password" class="input-group-text col-md-4" id="lb_password">λΉλ°λ²νΈ</label>
<input type="password" class="form-control col-md-8"
name="password" id="password" placeholder="λΉλ°λ²νΈ" required>
</div>
<div class="input-group mb-3 row">
<label for="password_confirm" class="input-group-text col-md-4" id="lb_password_confirm">λΉλ°λ²νΈ νμΈ</label>
<input type="password" class="form-control col-md-8"
name="password_confirm" id="password_confirm" placeholder="λΉλ°λ²νΈ νμΈ" required>
</div>
<div class="input-group mb-3 row">
<label for="name" class="input-group-text col-md-4" id="lb_name">μ΄λ¦</label>
<input type="text" class="form-control col-md-8"
name="name" id="name" placeholder="μ΄λ¦" required>
</div>
<div class="input-group mb-3 row">
<label for="email" class="input-group-text col-md-4" id="lb_email">μ΄λ©μΌ</label>
<input type="text" class="form-control col-md-8"
name="email" id="email" placeholder="μ΄λ©μΌ">
</div>
<div class="d-grid gap-2 mt-5 mb-5 d-md-flex justify-content-md-between">
<a href="javascript: history.back()" class="btn btn-lg btn-secondary">μ·¨μ</a>
<input type="button" class="btn btn-lg btn-primary" value="κ°μ
" onclick="checkUser()" />
</div>
</form>
</div>
<jsp:include page="/layout/main/UI/footer.jsp" />
<!-- script -->
<jsp:include page="/layout/main/import/script.jsp" />
<script>
async function clickIdCheck() {
let check = await idCheck()
if( check ) {
alert('μ€λ³΅λ μμ΄λ μ
λλ€.')
} else {
alert('μ¬μ© κ°λ₯ν μμ΄λ μ
λλ€.')
}
}
</script>
</body>
</html>
HTML
볡μ¬
μ ν¨μ± κ²μ¬ μ½λ μμ±
+μμ΄λ μ€λ³΅ κ²μ¬
β’
script.js
/**
* javascript - script.js
*/
/**
* νμ κ°μ
- μ ν¨μ± κ²μ¬
*/
async function checkUser() {
let form = document.joinForm
// μμ΄λ μ€λ³΅ νμΈ
let idDuplicated = await idCheck()
if( idDuplicated ) {
alert('μ€λ³΅λ μμ΄λ μ
λλ€.')
return false
}
if( validateUser() ) {
form.submit()
// μ ν¨μ± κ²μ¬ ν΅κ³Ό
return true
}
}
function validateUser() {
let form = document.joinForm
let username = form.username
let password = form.password
let password_confirm = form.password_confirm
let name = form.name
let email = form.email
let msg = ''
// μμ΄λ μ ν¨μ± κ²μ¬
let usernameCheck = /^[a-zA-Z0-9γ±-γ
γ
-γ
£κ°-ν£]{5,20}$/
msg = 'μμ΄λλ νκΈ, μλ¬Έμ, μ«μ 5~20 μλ‘ μ
λ ₯ν΄μ£ΌμΈμ.'
if (!check(usernameCheck, username, msg)) return false
// λΉλ°λ²νΈ μ ν¨μ± κ²μ¬
let passwordCheck = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
msg = 'λΉλ°λ²νΈλ μλ¬Έμ, μ«μ, νΉμλ¬Έμλ₯Ό ν¬ν¨νμ¬ 8μ μ΄μμΌλ‘ μ
λ ₯ν΄μ£ΌμΈμ.'
if (!check(passwordCheck, password, msg)) return false
// λΉλ°λ²νΈ νμΈ κ²μ¬
msg = 'λΉλ°λ²νΈμ λΉλ°λ²νΈ νμΈμ΄ λΆμΌμΉν©λλ€.'
if( password.value != password_confirm.value ) {
alert(msg)
return false
}
// μ΄λ¦ μ ν¨μ± κ²μ¬
let nameCheck = /^[a-zA-Zγ±-γ
γ
-γ
£κ°-ν£]{1,20}$/
msg = 'μ΄λ¦μ νκΈ λλ μλ¬Έ 20μ μ΄λ΄λ‘ μ
λ ₯ν΄μ£ΌμΈμ.'
if (!check(nameCheck, name, msg)) return false
// μ΄λ©μΌ μ ν¨μ± κ²μ¬
let emailCheck = /^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i
msg = 'μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.'
if (!check(emailCheck, email, msg)) return false
return true
}
// μ κ·ννμ μ ν¨μ± κ²μ¬ ν¨μ
function check(regExp, element, msg) {
if(regExp.test(element.value)) {
return true
}
alert(msg)
element.select()
element.focus()
return false
}
// μμ΄λ μ€λ³΅ νμΈ
function idCheck() {
let username = document.getElementById('username').value
let url = `${root}/users/idCheck`
let data = {
'username' : username,
}
return new Promise((resolve, reject) => {
// jQuery λ‘ AJAX μμ²
$.ajax({
type : 'GET', // μμ² λ©μλ
url : url, // μμ² URL
data : data, // μμ² λ°μ΄ν°
contentType : 'application/json', // μμ² λ°μ΄ν° νμ
dataType : 'html', // μλ΅ λ°μ΄ν° νμ
// μμ² μ±κ³΅
success : function(response, status) {
// response : μλ΅ λ°μ΄ν°
// status : μλ΅ μν
if( response == '' ) {
alert('μλ΅ λ©μμ§κ° μμ΅λλ€')
}
resolve(response == 'true' ? true : false)
},
// μλ¬
error : function(xhr, status) {
// xhr : XMLHttpRequest κ°μ²΄
// status : μλ΅ μν
alert('μλ¬ λ°μ')
reject('error')
}
})
})
}
JavaScript
볡μ¬
JDBConnection.java - JDBC λλΌμ΄λ² μ°λ
β’
JDBConnection.java
package common.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBConnection {
public Connection con; // μ°κ²°λ λλΌμ΄λ²μ SQLμ μμ²ν κ°μ²΄λ₯Ό μμ±νλ ν΄λμ€
public Statement stmt; // SQL μ€ν μμ²μ νλ ν΄λμ€
public PreparedStatement psmt; // Statement μμ ? νλΌλ―Έν° νμ₯κΈ°λ₯μ μΆκ°λ‘ μ 곡νλ ν΄λμ€
public ResultSet rs; // SQL μ€ν κ²°κ³Όλ₯Ό λ°μμ€λ ν΄λμ€
// κΈ°λ³Έ μμ±μ
public JDBConnection() {
// JDBC λλΌμ΄λ² λ‘λ
// Oracle
try {
// ojdbc8.jar λλΌμ΄λ²μ ν΄λμ€λ₯Ό λ‘λνλ€.
Class.forName("oracle.jdbc.OracleDriver");
// DBμ μ°κ²°
// jdbc:oracle:thin - JDBC λλΌμ΄λ² νμ
(thin νμ
)
// localhost - νΈμ€νΈ μ£Όμ (IP μ£Όμ), localhost λλ 127.0.0.1 μ νμ¬ PCμ IP λ₯Ό κ°λ¦¬ν¨λ€.
// :1521 - ν¬νΈλ²νΈ (1521μ μ€λΌν΄ DBμλ²μ κΈ°λ³Έ ν¬νΈ λ²νΈμ΄λ€.)
// :xe λλ :orcl - SID (μλΉμ€ ID)
//String url ="jdbc:oracle:thin:@localhost:1521:xe"; // 11g
// String url ="jdbc:oracle:thin:@localhost:1521:orcl"; // 12cμ΄μ
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String id = "aloha";
String pw = "123456";
// μλ° νλ‘κ·Έλ¨μμ JDBC λλΌμ΄λ²λ₯Ό μ°κ²°μμΌμ£Όλ ν΄λμ€
// getConnection() λ©μλλ‘ DBμ μ°κ²° μμ²νκ³ μμ±λ Connection κ°μ²΄λ₯Ό λ°νλ°λλ€.
con = DriverManager.getConnection(url, id, pw);
// System.out.println("DB μ°κ²° μ±κ³΅");
} catch (Exception e) {
System.err.println("DB μ°κ²° μ€ν¨");
e.printStackTrace();
}
}
}
Java
볡μ¬
UserDAO.java - λ°μ΄ν° μ κ·Ό κ°μ²΄ μμ±
β’
UserDAO.java
package users.data;
import java.sql.SQLException;
import java.util.UUID;
import common.jdbc.JDBConnection;
import users.model.PersistentLogins;
import users.model.Users;
public class UserDAO extends JDBConnection {
/**
* νμ κ°μ
* @param user λ±λ‘ν νμ μ 보
* @return λ±λ‘λ λ°μ΄ν° κ°μ
*/
public int join(Users user) {
int result = 0;
String sql = "INSERT INTO users (id, username, password, name, email, reg_date, upd_date) " +
"VALUES (RAWTOHEX(SYS_GUID()), ?, ?, ?, ?, sysdate, sysdate)";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, user.getUsername());
psmt.setString(2, user.getPassword());
psmt.setString(3, user.getName());
psmt.setString(4, user.getEmail());
result = psmt.executeUpdate();
} catch (SQLException e) {
System.err.println("νμ κ°μ
μ€ μ€λ₯ λ°μ");
e.printStackTrace();
}
return result;
}
}
Java
볡μ¬
UserService.java - μλΉμ€ μΈν°νμ΄μ€ μμ±
β’
UserService.java
package users.service;
import users.model.Users;
public interface UserService {
// νμκ°μ
public int join(Users user);
}
Java
볡μ¬
UserServiceImpl.java - μλΉμ€ ꡬν ν΄λμ€ μμ±
β’
UserServiceImpl.java
package users.service;
import users.data.UserDAO;
import users.model.PersistentLogins;
import users.model.Users;
public class UserServiceImpl implements UserService {
private UserDAO userDAO = new UserDAO();
@Override
public int join(Users user) {
int result = userDAO.join(user);
if( result > 0 ) {
System.out.println("νμ κ°μ
μ²λ¦¬ μ±κ³΅!");
} else {
System.out.println("νμ κ°μ
μ€ν¨!");
}
return result;
}
}
Java
볡μ¬
join_pro.jsp (νμ κ°μ μ²λ¦¬) νμ΄μ§ μμ±
β’
join_pro.jsp
<%--
νμ κ°μ
μ²λ¦¬
--%>
<%@page import="users.service.UserService"%>
<%@page import="users.service.UserServiceImpl"%>
<%@page import="users.model.Users"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Users user = new Users();
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String email = request.getParameter("email");
user.setUsername(username);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
// νμ μ 보 λ±λ‘ μμ²
UserService userService = new UserServiceImpl();
int result = userService.join(user);
String root = request.getContextPath();
if( result > 0 ) {
// λ‘κ·ΈμΈ νλ©΄μΌλ‘ μ΄λ
response.sendRedirect(root + "/users/login.jsp");
} else {
// νμκ°μ
νμ΄μ§λ‘ μ΄λ
response.sendRedirect(root + "/users/join.jsp?error");
}
%>
Java
볡μ¬
ν μ€νΈ
(μΆκ°μ€)