회원 가입시 아이디 중복 확인 추가 (내 프로젝트에 적용)

Back-End/Spring 2019. 9. 17. 13:59
728x90
반응형

회원가입 페이지에서 아이디 "중복확인" 버튼을 누르면 중복된 아이디가 있을시에는 경고창이 출력되고,


중복된 아이디가 없을시에는 아이디를 사용할 수 있다고 출력됨.



join.jsp (회원가입 페이지)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<%@ include file="../include/header.jsp"%>
<%@ include file="../include/menu.jsp"%><br>
<!-- 회원가입 페이지 -->
<body>
<center>
<table border="1" width="450" height="400">
    
        <br> <br>
        <center>
        <span style="color: green; font-weight: bold;">회원가입</span> <br> <br>
        
        <div style="text-align:center;">
            <tr>        
                <td>
                    
                        <!-- 받아온 아이디가 없을때, 즉 처음 이 페이지가 출력되었을 때-->
                        
                        <c:if test = "${user_id == null}">
                        <form action="join_id_check.do${e_mail}" method="post">
                    <center>
                        <div>    
                            아이디 : <input type="text" name="user_id" placeholder="  ID를 입력하세요. ">  <button type="submit" name="submit">중복확인</button>
                            
                            
                                                    
                        </div>
                        <br>
                        <div>
                            비밀번호 : <input type="password" name="member_pass"
                                placeholder="  비밀번호를 입력하세요. ">
                        </div>
                        <br>
                        <div>
                            인증받은 이메일 : ${e_mail}
                        </div>                        
                        
                        <!-- 이메일은 인증받은 이메일을 사용해야 하므로 컨트롤러에서 이메일을 가져와서 사용함 -->
                        <!-- 가져온후에 다시 컨트롤러로 넘긴후에 db에 저장하는 식으로 진행 -->
                        
                        <br> <br>
                        <button type="submit" name="submit">회원가입</button>
                    </center>
                        </div>
                    </td>
                </tr>
            </table>
            </center>
        </form>
                            </c:if>

                     <!-- 아이디 중복확인을 한 후에 아이디를 받아왔을 경우에 출력되는 부분 -->

                        <c:if test = "${user_id != null}">
                        <form action="join_check.do${user_id},${e_mail}" method="post">
                    <center>
                        <div>
                            아이디 : ${user_id}
                                                    
                        </div>
                        <br>
                        <div>
                            비밀번호 : <input type="password" name="member_pass"
                                placeholder="  비밀번호를 입력하세요. ">
                        </div>
                        <br>
                        <div>
                            인증받은 이메일 : ${e_mail}
                        </div>                        
                        
                        <!-- 이메일은 인증받은 이메일을 사용해야 하므로 컨트롤러에서 이메일을 가져와서 사용함 -->
                        <!-- 가져온후에 다시 컨트롤러로 넘긴후에 db에 저장하는 식으로 진행 -->
                        
                        <br> <br>
                        <button type="submit" name="submit">회원가입</button>
                    </center>
                        </div>
                    </td>
                </tr>
            </table>
            </center>
        </form>
                            </c:if>    
        </center>
<br><br><%@ include file="../include/Botton.jsp"%>
</body>
</html>
cs




MemberController.java 중 일부


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//id 중복확인을 하는 메소드
    @RequestMapping("/member/join_id_check.do{e_mail}")
    public ModelAndView id_check(String user_id, HttpServletResponse response_equals, @PathVariable String e_mail) throws Exception    {
        
        
        memberservice.join_id_check(user_id);
        
    //id가 기존 db에 저장되어 있지 않을 경우 실행되는 부분
    if(memberservice.join_id_check(user_id)) {
        
        response_equals.setContentType("text/html; charset=UTF-8");
        PrintWriter out_equals = response_equals.getWriter();
        out_equals.println("<script>alert('사용하실 수 있는 아이디 입니다.');</script>");
        out_equals.flush();
        
        ModelAndView mv = new ModelAndView();
        
        mv.setViewName("/member/join");
        
        mv.addObject("e_mail", e_mail);
        
        mv.addObject("user_id",user_id);
        
        return mv;
        
    //id가 기존 db에 저장되어 있을 경우 id가 중복된 것으므로 이쪽 구문이 실행된다.
    
else {

        response_equals.setContentType("text/html; charset=UTF-8");

        PrintWriter out_equals = response_equals.getWriter();

        out_equals.println("<script>alert('사용할 수 없는 아이디 입니다. 다른 아이디를 입력해주세요.'); history.go(-1);</script>");
        
out_equals.flush();
        
    }
    
    ModelAndView mv = new ModelAndView();

    mv.setViewName("/member/join");
    
        return mv;
    }

cs



MemberServiceImpl.java 중 일부


1
2
3
4
5
6
7
8

//아이디 중복 확인
    @Override
    public boolean join_id_check(String user_id) throws Exception {
    
//참, 거짓을 판별해서 리턴함
        boolean result = memberdao.join_id_check(user_id);
        
        return result;
    }
cs



MemberDAOImpl.java 중 일부


삼항 연산자를 사용해서 user의 id가 기존에 저장되어 있는 데이터라면 false를 리턴하고,


기존에 저장되어 있지 않은 데이터라면 true를 리턴하게 한다.


1
2
3
4
5
6
7
8
9
@Override
    public boolean join_id_check(String user_id) throws Exception {
 
        String user_id1 = sqlSession.selectOne("member.join_id_check", user_id);
    
        //조건식 ? true일때의 값 : false일때의 값

        return (user_id1==null) ? true : false;
    }
    
cs



MemberMapper.xml 중 일부


1
2
3
4
5
    
<!-- 아이디 중복확인 관련 mapper-->

    <select id = "join_id_check" resultType = "String" >
        select user_id from member
        where user_id=#{user_id}

    </select>       
cs




728x90
반응형
: