
2025λ 4μ 3μΌ
νμκ°μ κΈ°λ₯μ μΉ μ ν리μΌμ΄μ μμ κ°μ₯ κΈ°λ³Έμ΄μ ν΅μ¬μ΄λ€.
μ΄λ² μ€μ΅μμλ JSPμ Oracle DBλ₯Ό κΈ°λ°μΌλ‘ νμκ°μ νλ¦ β μ ν¨μ± κ²μ¬ β μ€λ³΅ νμΈ β DB μ μ₯ β μΈμ λ±λ‘ β λ‘κ·ΈμΈ νμ΄μ§ μ΄λκΉμ§λ₯Ό μμ±νλ€.
join_form.jspμμ μ¬μ©μ μ
λ ₯join.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>
<%@ 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) {}
}
%>
session.setAttribute("isAuth", true); // λ‘κ·ΈμΈ μ¬λΆ
session.setAttribute("role", "ROLE_ADMIN"); // μ¬μ©μ κΆν
session.setMaxInactiveInterval(30); // μΈμ
μ μ§ μκ°
β μ΄ μΈμ κ°λ€μ λ€λ₯Έ νμ΄μ§μμλ λ‘κ·ΈμΈ μν μ 무 νλ¨ μ νμ©λ¨
β οΈ νμ¬ μ€μ΅μμλ λΉλ°λ²νΈλ₯Ό νλ¬Έ κ·Έλλ‘ DBμ μ μ₯
β μ€λ¬΄μμλ λ°λμ
SHA-256,BCryptκ°μ μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ μνΈν ν μ μ₯ν΄μΌ νλ€.
μ΄λ² μ€μ΅μ ν΅ν΄ JSPμμμ μ λ ₯κ° μ²λ¦¬, DB μ°λ, μ€λ³΅ νμΈ, μΈμ κ΄λ¦¬, νμ΄μ§ μ΄λ νλ¦μ
μ 체μ μΌλ‘ 체λν μ μμλ€.
νΉν μΈμ μ μ΄μ©ν μΈμ¦ μ²λ¦¬, μ ν¨μ± μ²΄ν¬ ν μμΈ μ μ΄, μ€λ³΅ μ¬μ©μ μ²λ¦¬ νλ¦ λ±μ
μ€μ μΉ νλ‘μ νΈμμλ μμ£Ό λ±μ₯νλ λΆλΆμ΄λΌ λ§€μ° μ μ΅νλ€.
request.getParameter()λ‘ μ¬μ©μ μ
λ ₯ μμ PreparedStatement + μμΈ μ²λ¦¬ νμ