개발/WEB개발
JSP 회원가입 만들기
DoR@Hee
2019. 1. 31. 01:50
View - register.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <html> <head> <script charset="utf-8" type="text/javascript" src="Check.js"></script> <meta charset="utf-8"> <link rel="stylesheet" href="style.css?ver=1.1"> <link href="https://fonts.googleapis.com/css?family=Gamja+Flower&subset=korean" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Mukta" rel="stylesheet"> </head> <body> <form action="registerOk.jsp" method="post" name="regChk"> <div id="registerbox"> <p id="signup">sign up</p> <label><span class="star">*</span>아이디</label><input id="id"type="text" name="uId"><br> <label><span class="star">*</span>패스워드</label><input id="password"type="password" name="uPassword"><br> <p id="passwordrole">비밀번호는 8자리 이상이어야 하며 영문,숫자,패스워드를 반드시 포함해야 합니다.</p> <label><span class="star">*</span>패스워드 확인</label><input id="password2"type="password" name="uPassword2"><br> <button type="button" id="login_btn" name="register_btn" onclick="registerChk();">회원가입</button> </div> </form> </body> </html> | cs |
controller - register_ok.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 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@page import="com.javalec.ex.dto.*"%> <%@page import="com.javalec.ex.dao.*"%> <% request.setCharacterEncoding("EUC-KR"); %> <% String uId = request.getParameter("uId"); String uPassword = request.getParameter("uPassword"); UDao dao = UDao.getInstance(); int chkNum = dao.userIdChk(uId); if(chkNum==-1) { %> <script> alert("이미 존재하는 아이디 입니다."); history.back(); </script> <% } else if(chkNum==0){ int ri = dao.registerOk(uId,uPassword); if(ri == 0){ session.setAttribute("uId",uId); %> <script language="javascript"> alert("회원가입을 축하 합니다."); document.location.href="login.jsp"; </script> <% } } %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> </body> </html> | cs |
Model - UDao.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 | public int registerOk(String uId, String uPassword) { //입력받은 id와 pw를 db에 저장 int ri = 0; Connection con = null; PreparedStatement pstmt = null; try { String query="insert into members values (?,?)"; con=DriverManager.getConnection(url, user, password); pstmt=con.prepareStatement(query); pstmt.setString(1, uId); pstmt.setString(2, uPassword); pstmt.executeUpdate(); ri = 0; //회원가입 성공 } catch (Exception e) { e.printStackTrace(); } finally { try { if(pstmt != null) pstmt.close(); if(con != null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } return ri; } | cs |
Model(2) - UDao.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 | public int userIdChk(String uId) { // 입력받은 user 의 id와 db의 id 비교 (중복확인) int ri = 0; Connection con = null; PreparedStatement pstmt = null; ResultSet res = null; try { String query = "select * from members where id =?"; con = DriverManager.getConnection(url, user, password); pstmt=con.prepareStatement(query); pstmt.setString(1, uId); res = pstmt.executeQuery(); if(res.next()) { ri= -1; // 이미 존재하는 아이디 } else { ri = 0; // 존재하지않은 아이디(회원가입 성공) } } catch(Exception e) { e.printStackTrace(); } finally { try { if(con != null) con.close(); if(res != null) res.close(); if(pstmt != null) pstmt.close(); }catch(Exception e2) { e2.printStackTrace(); } } return ri; } | cs |
완성ㅍ
완성 페이지