πŸ“Œ JSP νšŒμ›κ°€μž… κΈ°λŠ₯ κ΅¬ν˜„ (Oracle + μ„Έμ…˜ + μœ νš¨μ„± 검사 흐름 μ™„μ „ 정리)

My Pale Blue DotΒ·2025λ…„ 4μ›” 3일
0

JSP/SERVLET

λͺ©λ‘ 보기
7/25
post-thumbnail

πŸ“… λ‚ μ§œ

2025λ…„ 4μ›” 3일


πŸ“ ν•™μŠ΅ λ‚΄μš©


βœ… 1. νšŒμ›κ°€μž… ν”„λ‘œμ„ΈμŠ€ κ°œμš”

νšŒμ›κ°€μž… κΈ°λŠ₯은 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ κ°€μž₯ 기본이자 핡심이닀.

이번 μ‹€μŠ΅μ—μ„œλŠ” JSP와 Oracle DBλ₯Ό 기반으둜 νšŒμ›κ°€μž… 흐름 β†’ μœ νš¨μ„± 검사 β†’ 쀑볡 확인 β†’ DB μ €μž₯ β†’ μ„Έμ…˜ 등둝 β†’ 둜그인 νŽ˜μ΄μ§€ μ΄λ™κΉŒμ§€λ₯Ό μ™„μ„±ν–ˆλ‹€.


🧭 2. νšŒμ›κ°€μž… 전체 흐름도

πŸ“Œ 흐름 μš”μ•½:

  1. join_form.jspμ—μ„œ μ‚¬μš©μž μž…λ ₯
  2. join.jsp둜 μ „μ†‘λœ 데이터
    • μœ νš¨μ„± 검사
    • 쀑볡 ID 확인
    • DB INSERT
    • μ„Έμ…˜ 생성
    • 둜그인 νŽ˜μ΄μ§€λ‘œ 이동


🧾 3. νšŒμ›κ°€μž… 폼 (join_form.jsp)

<%
if (session.getAttribute("isAuth") != null) {
    // 이미 둜그인된 경우 메인 νŽ˜μ΄μ§€λ‘œ 이동
    response.sendRedirect("./main.jsp");
    return;
}
%>

<form action="${pageContext.request.contextPath}/C06/03/join.jsp" method="post">
	<input type="text" name="userid" /><br/>
	<input type="text" name="password" /><br/>
	<input type="text" name="username" /><br/>
	<input type="submit" value="νšŒμ›κ°€μž…" />
</form>

πŸ› οΈ 4. νšŒμ›κ°€μž… 처리 둜직 (join.jsp)

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>

<%
request.setCharacterEncoding("UTF-8");

// 1. μ‚¬μš©μž μž…λ ₯ μˆ˜μ‹ 
String userid = request.getParameter("userid");
String password = request.getParameter("password");
String username = request.getParameter("username");

// 2. μž…λ ₯ μœ νš¨μ„± 검사
boolean hasError = false;
if (userid == null || userid.trim().isEmpty()) {
    out.println("IDλ₯Ό μž…λ ₯ν•˜μ„Έμš”.<br>");
    hasError = true;
}
if (password == null || password.trim().isEmpty()) {
    out.println("PWλ₯Ό μž…λ ₯ν•˜μ„Έμš”.<br>");
    hasError = true;
}
if (username == null || username.trim().isEmpty()) {
    out.println("이름을 μž…λ ₯ν•˜μ„Έμš”.<br>");
    hasError = true;
}
if (hasError) return;

// 3. Oracle DB μ—°κ²° 정보
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String dbUser = "system";
String dbPassword = "1234";

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
    // 4. λ“œλΌμ΄λ²„ λ‘œλ“œ 및 μ—°κ²°
    Class.forName(driver);
    conn = DriverManager.getConnection(url, dbUser, dbPassword);

    // 5. 쀑볡 ID 체크
    String checkSql = "SELECT COUNT(*) FROM member WHERE user_id = ?";
    pstmt = conn.prepareStatement(checkSql);
    pstmt.setString(1, userid);
    rs = pstmt.executeQuery();

    boolean isDuplicate = false;
    if (rs.next()) {
        int count = rs.getInt(1);
        if (count > 0) isDuplicate = true;
    }

    rs.close(); pstmt.close();

    if (isDuplicate) {
        out.println("<script>alert('이미 μ‘΄μž¬ν•˜λŠ” κ³„μ •μž…λ‹ˆλ‹€.'); location.href='join_form.jsp';</script>");
        return;
    }

    // 6. νšŒμ› INSERT 쿼리 μ‹€ν–‰
    String insertSql = "INSERT INTO member (user_id, user_pw, user_name) VALUES (?, ?, ?)";
    pstmt = conn.prepareStatement(insertSql);
    pstmt.setString(1, userid);
    pstmt.setString(2, password); // ⚠️ 싀무에선 λ°˜λ“œμ‹œ μ•”ν˜Έν™”!
    pstmt.setString(3, username);

    int result = pstmt.executeUpdate();

    // 7. μ„Έμ…˜ 등둝 (INSERT 성곡 μ‹œμ—λ§Œ)
    if (result > 0) {
        session.setAttribute("isAuth", true);
        session.setAttribute("role", "ROLE_ADMIN");
        session.setMaxInactiveInterval(30); // 30초 μœ μ§€

        out.println("<script>alert('login page둜 μ΄λ™ν•©λ‹ˆλ‹€.'); location.href='login.jsp';</script>");
    } else {
        out.println("νšŒμ›κ°€μž… μ‹€νŒ¨!");
    }

} catch (Exception e) {
    e.printStackTrace();
    out.println("였λ₯˜ λ°œμƒ: " + e.getMessage());
} finally {
    try { if (rs != null) rs.close(); } catch (Exception e) {}
    try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}
    try { if (conn != null) conn.close(); } catch (Exception e) {}
}
%>

πŸ” 5. μ„Έμ…˜ 처리 핡심 포인트

session.setAttribute("isAuth", true);      // 둜그인 μ—¬λΆ€
session.setAttribute("role", "ROLE_ADMIN"); // μ‚¬μš©μž κΆŒν•œ
session.setMaxInactiveInterval(30);        // μ„Έμ…˜ μœ μ§€ μ‹œκ°„

β†’ 이 μ„Έμ…˜ 값듀은 λ‹€λ₯Έ νŽ˜μ΄μ§€μ—μ„œλ„ 둜그인 μƒνƒœ 유무 νŒλ‹¨ μ‹œ ν™œμš©λ¨


🚨 6. λ³΄μ•ˆ 팁 – λΉ„λ°€λ²ˆν˜Έ μ•”ν˜Έν™” 처리

⚠️ ν˜„μž¬ μ‹€μŠ΅μ—μ„œλŠ” λΉ„λ°€λ²ˆν˜Έλ₯Ό 평문 κ·ΈλŒ€λ‘œ DB에 μ €μž₯

βœ… μ‹€λ¬΄μ—μ„œλŠ” λ°˜λ“œμ‹œ SHA-256, BCrypt 같은 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ μ•”ν˜Έν™” ν›„ μ €μž₯ν•΄μ•Ό ν•œλ‹€.


πŸ’¬ λŠλ‚€ 점

이번 μ‹€μŠ΅μ„ 톡해 JSPμ—μ„œμ˜ μž…λ ₯κ°’ 처리, DB 연동, 쀑볡 확인, μ„Έμ…˜ 관리, νŽ˜μ΄μ§€ 이동 흐름을

μ „μ²΄μ μœΌλ‘œ 체득할 수 μžˆμ—ˆλ‹€.

특히 μ„Έμ…˜μ„ μ΄μš©ν•œ 인증 처리, μœ νš¨μ„± 체크 ν›„ μ˜ˆμ™Έ μ œμ–΄, 쀑볡 μ‚¬μš©μž 처리 흐름 등은

μ‹€μ œ μ›Ή ν”„λ‘œμ νŠΈμ—μ„œλ„ 자주 λ“±μž₯ν•˜λŠ” 뢀뢄이라 맀우 μœ μ΅ν–ˆλ‹€.


πŸ“Œ μš”μ•½

  • νšŒμ›κ°€μž…μ€ 폼 β†’ μœ νš¨μ„± 검사 β†’ 쀑볡 확인 β†’ INSERT β†’ 둜그인 이동 μˆœμ„œλ‘œ 처리됨
  • request.getParameter()둜 μ‚¬μš©μž μž…λ ₯ μˆ˜μ‹ 
  • DB 연동은 PreparedStatement + μ˜ˆμ™Έ 처리 ν•„μˆ˜
  • μ„Έμ…˜ 등둝은 λ°˜λ“œμ‹œ μ„±κ³΅ν•œ κ²½μš°μ—λ§Œ μˆ˜ν–‰
  • λΉ„λ°€λ²ˆν˜ΈλŠ” λ°˜λ“œμ‹œ μ•”ν˜Έν™”ν•΄μ„œ μ €μž₯ν•΄μ•Ό 함

profile
Here, My Pale Blue.🌏

0개의 λŒ“κΈ€