디지털 컨버전스/Project

[BoardProject] - 회원가입

gimyeondong 2020. 4. 28. 18:38

-정규표현식

-비밀번호 일치

-암호화

BoardProject.zip
1.98MB


오라클 코드

오라클 코드
create table member (
    id varchar(20) primary key,
    pw varchar(128) not null,
    name  varchar(20) not null,
    phone  varchar(20),
    email  varchar(50),
    zipcode  varchar(7),
    address1  varchar(100),
    address2  varchar(100)  
);
commit;
select *from member;

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
    </head>
    <body>
        <div class="wrapper">
            <table align=center border="1px" style="text-align: center">
                <tr>
                    <td><b>Login</b></td>
                </tr>
                <tr>
                    <td><input type=text placeholder="Input your ID" ></td>
                </tr>
                <tr>
                    <td><input type="password" placeholder="Input your PW" ></td>
                </tr>
                <tr>
                    <td>
                        <input type="button" id="login" value="Login">
                        <input type="button" id="signup" value="Sign up"><br>
                        <input type="checkbox">Remember my ID
                    </td>
                </tr>
            </table>
        </div>
        <script>
            $("#signup").on("click",function(){
                location.href="signup.jsp"
            })

        </script>
    </body>
</html>

signup.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
        <script src="https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
        <style>
            *{box-sizing: border-box;}
            .wrapper div{
                border: 1px solid black;
                margin: 0px;
                padding: 1px;
            }
            .wrapper{
                width:500px;
                margin: auto;
            }
            .right{
                text-align: right;
            }
        </style>
        <script>
            window.onload = function(){
                document.getElementById("check").onsubmit = function(){
                    var id = document.getElementById("id").value;
                    var regexid = /[A-Za-z]{8,12}/;
                    var checkid = regexid.test(id);
                    if(!checkid){
                        alert("아이디는 8~12글자의 알파벳 대소문자로 만들어야 합니다.")
                    }
                    var pw1 = document.getElementById("pw1").value;
                    var regexpw1= /.{6,12}/;
                    var resultpw1 = regexpw1.test(pw1);
                    if(!resultpw1){
                        alert("비밀번호 입력값이 잘못되었습니다. 공백문자를 제외하고 6글자 이상 12글자 이하로 입력하세요")
                    }

                    var pw2 = document.getElementById("pw2").value;
                    if(pw1 != pw2){
                        alert("비밀번호 입력값이 서로 다릅니다.")
                    }

                    var name = document.getElementById("name").value;
                    var regexname= /[A-Za-z가-힣]{1,15}/;
                    var resultname = regexname.test(name);
                    if(!resultname){
                        alert("이름 입력값이 잘못되었습니다. 1~15글자의 알파벳 대소문자 및 한글로 입력하세요")
                    }
                    var phone = document.getElementById("phone").value;
                    var regexphone= /\d{10,11}/;
                    var resultphone = regexphone.test(phone);
                    if(!resultphone){
                        alert("전화번호 입력값이 잘못되었습니다. 숫자 10~11자로 입력하세요")
                    }

                    var email = document.getElementById("email").value;
                    var regexemail= /.+@[a-z]+\.[a-z]+/;
                    var resultemail = regexemail.test(email);
                    if(!resultemail){
                        alert("이메일 입력값이 잘못되었습니다. @와 . 을 포함하여 입력해주세요")
                    } 
                   return resultid && resultpw1 && resultname && resultphone && email 

                }
            }
        </script>
    </head>
    <body>
        <div class="wrapper">
            <form id=check action=SignupController method="post">
                <div class="row">
                    <div class="col-sm-12"  align=center>회원가입</div>
                </div>

                <div class="row">
                    <div class="col-3 right">
                        아이디 :
                    </div>
                    <div class="col">
                        <div>
                            <input type="text" placeholder="8~12글자의 알파벳 대소문자" id="id" name="id">
                            <input type="button" value="중복확인">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-3 right">패스워드 :</div>
                    <div class="col">
                        <input type="text" id="pw1" name="pw">
                    </div>
                </div>
                <div class="row">
                    <div class="col-3 right">패스워드 확인 :</div>
                    <div class="col">
                        <input type="text" id="pw2">
                    </div>
                    <div id="pwcheck"></div>
                </div>
                <div class="row">
                    <div class="col-3 right">이름 :</div>
                    <div class="col"><input type="text" id="name" name="name"></div>
                </div>
                <div class="row">
                    <div class="col-3 right">전화번호 :</div>
                    <div class="col"><input type="text" id="phone" name="phone"></div>
                </div>
                <div class="row">
                    <div class="col-3 right">이메일 :</div>
                    <div class="col"><input type="text" id="email" name="email"></div>
                </div>
                <div class="row">
                    <div class="col-3 right">우편번호 :</div>
                    <div class="col">
                        <input type="text"  id="postcode" name="zipcode" readonly>
                        <input type="button" value="찾기" onclick="sample4_execDaumPostcode()">
                    </div>
                </div>
                <div class="row">
                    <div class="col-3 right">주소1 :</div>
                    <div class="col"><input type="text" id="address1" name="address1" readonly></div>
                </div>
                <div class="row">
                    <div class="col-3 right">주소2 :</div>
                    <div class="col"><input type="text" id="address2" name="address2"></div>
                </div>
                <div class="row">
                    <div class="col" align=center>
                        <input type="submit" value="회원가입">
                        <input type="button" value="다시 입력">
                    </div>
                </div>
            </form>
        </div>

        <script>
            document.getElementById("pw2").onkeyup	 = function(){
                if(document.getElementById("pw1").value==document.getElementById("pw2").value){
                    document.getElementById("pwcheck").innerHTML = "패스워드가 일치합니다"
                    document.getElementById("pwcheck").style.color = "blue";
                }else{
                    document.getElementById("pwcheck").innerHTML = "패스워드가 일치하지 않습니다"
                    document.getElementById("pwcheck").style.color = "red";
                }
            };
            function sample4_execDaumPostcode() {
                new daum.Postcode({
                    oncomplete: function(data) {
                        var roadAddr = data.roadAddress; 
                        document.getElementById('postcode').value = data.zonecode;
                        document.getElementById("address1").value = roadAddr;
                    }
                }).open();
            }
        </script>


    </body>
</html>

memberDTO.java

package kh.backend.dto;

public class memberDTO {
	private String id;
	private String pw;
	private String name; 
	private String phone; 
	private String email; 
	private String zipcode; 
	private String address1; 
	private String address2;
	
	public memberDTO() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public memberDTO(String id, String pw, String name, String phone, String email, String zipcode, String address1,
			String address2) {
		super();
		this.id = id;
		this.pw = pw;
		this.name = name;
		this.phone = phone;
		this.email = email;
		this.zipcode = zipcode;
		this.address1 = address1;
		this.address2 = address2;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPw() {
		return pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	} 
	
	
}

memberDAO.java

package kh.backend.dao;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import kh.backend.dto.memberDTO;


public class memberDAO {
	private Connection getConnection() throws Exception{
		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String id = "kh";
		String pw = "kh";
		Class.forName("oracle.jdbc.driver.OracleDriver");
		return DriverManager.getConnection(url,id,pw);
	}
	
	public int signup(memberDTO dto) throws Exception{
		String sql = "insert into member values(?,?,?,?,?,?,?,?)";
		
		try(Connection con = this.getConnection();	
		PreparedStatement pstat = con.prepareStatement(sql)){
			pstat.setString(1, dto.getId());
			pstat.setString(2, dto.getPw());
			pstat.setString(3, dto.getName());
			pstat.setString(4, dto.getPhone());
			pstat.setString(5, dto.getEmail());
			pstat.setString(6, dto.getZipcode());
			pstat.setString(7, dto.getAddress1());
			pstat.setString(8, dto.getAddress2());
			int result = pstat.executeUpdate();
			con.commit();
			return result;
		}
	}
	
	//암호화
    public static String getSHA512(String input){
		String toReturn = null;
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-512");
			digest.reset();
			digest.update(input.getBytes("utf8"));
			toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return toReturn;
	}
	
}

SignupController.java

package kh.backend.controller;

import java.io.IOException;
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 kh.backend.dao.memberDAO;
import kh.backend.dto.memberDTO;


@WebServlet("/SignupController")
public class SignupController extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		memberDAO dao = new memberDAO();
		String id = request.getParameter("id");
		String pw = dao.getSHA512(request.getParameter("pw"));
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String zipcode = request.getParameter("zipcode");
		String address1 = request.getParameter("address1");
		String address2 = request.getParameter("address1");
		
		try {
			int result = dao.signup(new memberDTO(id,pw,name,phone,email,zipcode,address1,address2));
			System.out.println(result);
			response.sendRedirect("index.jsp");
			

		}catch(Exception e) {
			e.printStackTrace();
			response.sendRedirect("error.jsp");
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}