상세 컨텐츠

본문 제목

간단한 회원가입 및 회원정보 수정

WebProgramming/JSP

by ChrisMare 2018. 10. 24. 15:40

본문

프로세스

참고사항 )

저는 OracleDriver를 사용하였습니다.

join.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Join</title>
</head>
<body>
    <h1>Join</h1>
    <hr>
    <form action="JoinOK" method="post">
        ID : <input type="text" name="join_id" size="10"><br />
        PW : <input type="text" name="join_pw" size="10"><br />
        Name : <input type="text" name="join_name" size="10"> <br />
        sex : <input type="radio" name="join_sex" value="남">남
        &nbsp;<input type="radio" name="join_sex" value="여">여 <br/>
        age : <input type="number" name="join_age" size="5" value="0"><br/><br/>
        <input type="submit" value="join">&nbsp;<input type="reset" value="reset">
    </form>
</body>
</html>
cs

JoinOK.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
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
92
93
94
95
96
97
package com.servletTest.ex;
 
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class JoinOK
 */
@WebServlet("/JoinOK")
public class JoinOK extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    private String id, pw, name, sex;
    private int age;
    
    private Connection conn;
    private Statement stmt;
    
    /**
     * Default constructor. 
     */
    public JoinOK() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("doGet");
        doAction(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("doPost");
        doAction(request, response);
    }
 
    private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doAction");
        
        request.setCharacterEncoding("UTF-8");
        
        id = request.getParameter("join_id");
        pw = request.getParameter("join_pw");
        name = request.getParameter("join_name");
        sex = request.getParameter("join_sex");
        age = Integer.parseInt(request.getParameter("join_age"));
        System.out.println(name);
        String query = "insert into member values('"+id+"','"+pw+"','"+name+"','"+sex+"',"+age+")";
        
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe""scott""1234");
            stmt = conn.createStatement();
            int isCheck = stmt.executeUpdate(query);
            
            if(isCheck == 1) {
                System.out.println(id + "Member Join Insert Success!!");
                response.sendRedirect("joinResult.jsp");
            }else {
                System.out.println(id + "Member Join Insert Fail!!");
                response.sendRedirect("join.html");
            }
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt != null) {
                    stmt.close();
                }
                if(conn != null) {
                    conn.close();
                }
            }catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }
    
}
 
cs

joinResult.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JoinResult</title>
</head>
<body>
<h1>JoinResult</h1>
<hr>
<span>회원가입에 성공하셨습니다.</span><br/>
<%
    System.out.println("JoinResult.jsp");
%>
<a href="login.html">로그인</a>
 
</body>
</html>
cs

login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <hr>
    <form action="LoginOK" method="post">
        <input type="text" size="10" name="id" placeholder="아이디를 입력해주세요."><br/>
        <input type="text" size="10" name="pw" placeholder="비밀번호를 입력해주세요."><br/>
        <input type="submit" value="로그인">&nbsp;<input type="reset" value="reset">
    </form>
</body>
</html>
cs

LoginOK.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
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
package com.servletTest.ex;
 
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
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 javax.servlet.http.HttpSession;
 
/**
 * Servlet implementation class LoginOK
 */
@WebServlet("/LoginOK")
public class LoginOK extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    
    private String id, pw, name, sex; 
    int age;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginOK() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doAction(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doAction(request, response);
    }
    
    private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        id = request.getParameter("id");
        pw = request.getParameter("pw");
        
        String query = "select * from member where id='" + id + "' and pw='" + pw + "'";
        
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe""scott""1234");
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            
            while(rs.next()) {
                id = rs.getString("id");
                pw = rs.getString("pw");
            }
            
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("id",id);
            httpSession.setAttribute("pw", pw);
            
            response.sendRedirect("loginResult.jsp");
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null) rs.close();
                if(stmt != null) stmt.close();
                if(conn != null) conn.close();
            }catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }
}
cs

loginResult.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%!
    String id, pw;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Login Result</title>
</head>
<body>
<h1>LoginResult</h1>
<hr>
<%
    System.out.println("Login Result");
    id = (String)session.getAttribute("id");
    pw = (String)session.getAttribute("pw");
%>
<span><%= id %>님 안녕하세요!</span><br/>
<a href="logout.jsp">로그아웃</a> &nbsp; <a href="modify.jsp">회원정보 수정</a>
</body>
</html>
cs

modify.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
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%!
    String id, pw, name, sex;
    int age;
    Connection conn;
    Statement stmt;
    ResultSet rs;
%>
 
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Modify</title>
</head>
<body>
<h1>회원정보 수정</h1>
<hr>
<%
    id = (String)session.getAttribute("id");
    
    String query = "select * from member where id='" + id + "'";
    
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe""scott""1234");
    stmt = conn.createStatement();
    rs = stmt.executeQuery(query);
    
    while(rs.next()){
        id = rs.getString("id");
        pw = rs.getString("pw");
        name = rs.getString("name");
        sex = rs.getString("sex");
        age = rs.getInt("age");
    }
    
%>
<form action="ModifyOK" method="post">
    아이디 : <%=id %><br/>
    비밀번호 : <input type="text" name="pw" readonly="readonly"><br/>
    이름 : <input type="text" name="name" value="<%=name%>"><br/>
    <%
        if(sex.equals("남")) {
    %>
    성별 : <input type="radio" name="sex" value="남" checked="checked"> 남 &nbsp;
         <input type="radio" name="sex" value="여" checked="checked"> 여 <br/>
    <%
        }else {
    %>
    성별 : <input type="radio" name="sex" value="남" checked="checked"> 남 &nbsp;
         <input type="radio" name="sex" value="여" checked="checked"> 여 <br/>
    <%
        }
    %>
    나이 : <input type="text" name="age" value="<%=age%>"></br>
    <input type="submit" value="수정">&nbsp;<input type="reset" value="취소">
</form>
</body>
</html>
cs

ModifyOK.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.servletTest.ex;
 
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
 
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/**
 * Servlet implementation class JoinOK
 */
@WebServlet("/ModifyOK")
public class ModifyOK extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    private String id, pw, name, sex;
    private int age;
    
    private Connection conn;
    private Statement stmt;
 
    
    private HttpSession httpSession;
    /**
     * Default constructor.
     */
    public ModifyOK() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("doGet");
        doAction(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("doPost");
        doAction(request, response);
    }
 
    private void doAction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("doAction");
        request.setCharacterEncoding("EUC-KR");
        
        httpSession = request.getSession();
 
        id = httpSession.getAttribute("id").toString();
        pw = request.getParameter("pw");
        name = request.getParameter("name");
        sex = request.getParameter("sex");
        age = Integer.parseInt(request.getParameter("age"));
        String query = "update member set id='"+ id +"', pw='"+ pw +"', name='"+ name +"', sex='"+ sex +"', age="+ age +" where id='"+ id + "'";
        System.out.println(query);
        
        if (pwConfirm()) {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe""scott""1234");
                stmt = conn.createStatement();
                int isCheck = stmt.executeUpdate(query);
 
                if (isCheck == 1) {
                    System.out.println(id + "Member Update Success!!");
                    response.sendRedirect("modifyResult.jsp");
                } else {
                    System.out.println(id + "Member Update Fail!!");
                    response.sendRedirect("modify.jsp");
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
        }else {
            System.out.println("pw error");
        }
    }
 
    private boolean pwConfirm() {
        boolean result = false;
        
        String sessionPw = (String)httpSession.getAttribute("pw");
        
        if(sessionPw.equals(pw)) result = true;
        
        return result;
    }
    
}
 
cs

modifyResult.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>ModifyResult</title>
</head>
<body>
<h1>ModifyResult</h1>
<hr>
<span>회원 정보 수정에 성공하셨습니다.</span><br/>
<%
    String id = (String)session.getAttribute("id");
%>
<%= id %> 님의 회원정보 수정이 완료되었습니다.<br/>
<a href="logout.jsp">로그아웃</a> &nbsp; <a href="modify.jsp">회원정보 수정</a>
 
</body>
</html>
cs

logout.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Logout</title>
</head>
<body>
<%
    session.invalidate();
    response.sendRedirect("login.html");
%>
</body>
</html>
cs


하지만 위의 로직 처럼 만들어나가면 코드가 복잡해지며 코드의 중복이 많이 나오게 됩니다.

따라서, 유지보수 및 코드의 모듈화를 위해서는 별도의 DAO 클래스를 만들어서 사용해야 됩니다.

또한 DTO를 이용하여 데이터 베이스의 데이터를 자바 객체로 바꿔줌으로써 관리하기 쉬워지며, 

PreparedStatement 객체를 사용하여 코드의 가독성을 주는 방법으로 다시 회원인증 프로그램을 만들어 보겠습니다.


< 바로가기 >



관련글 더보기

댓글 영역