완벽해! ⭐️
요청한 5가지 기능을 JSP 기반 로그인 시스템에 단계별로 구현하는 방법 정리해줄게.
기초부터 고급 기능까지, 하나씩 차근차근 추가할 수 있게 만들었어!
<%@ page import="java.io.*, java.text.SimpleDateFormat" %>
<%
// 로그아웃 시간 기록
String userId = (String)session.getAttribute("userId");
String logoutTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
// 로그 메시지 작성
String log = userId + " 님이 " + logoutTime + "에 로그아웃했습니다.\n";
// 파일 저장 (서버 경로 기준)
String path = application.getRealPath("/logs/access_log.txt");
FileWriter fw = new FileWriter(path, true); // append 모드
fw.write(log);
fw.close();
session.invalidate();
response.sendRedirect("logoutDone.jsp");
%>
🔥 더 고급으로 가면 DB 연동 (MySQL + JDBC) 도 가능해.
원하면 DB 테이블 스키마부터insert
코드까지 정리해줄게!
<script>
let remaining = 60 * 5; // 5분 = 300초
function updateTimer() {
let minutes = Math.floor(remaining / 60);
let seconds = remaining % 60;
document.getElementById("countdown").innerText =
`${minutes}분 ${seconds}초 남았습니다`;
if (remaining > 0) {
remaining--;
setTimeout(updateTimer, 1000);
} else {
alert("세션이 만료되었습니다! 다시 로그인해주세요.");
window.location.href = "sessionExpired.jsp";
}
}
window.onload = updateTimer;
</script>
<p id="countdown">세션 유지 시간 로딩 중...</p>
💡 JSP에서
session.setMaxInactiveInterval(300);
설정과도 맞춰야 함.
userId
, nickname
)<%
String userId = (String) session.getAttribute("userId");
String nickname = (String) session.getAttribute("nickname"); // 선택사항
%>
<% if (userId != null) { %>
<p>👤 현재 로그인 중: <strong><%= userId %></strong></p>
<% } else { %>
<p>🔒 로그인되지 않았습니다.</p>
<% } %>
login.jsp
<%
String error = request.getParameter("error");
if ("1".equals(error)) {
%>
<p style="color:red;">❌ 아이디 또는 비밀번호가 올바르지 않습니다.</p>
<%
}
%>
<form method="post" action="loginProcess.jsp">
ID: <input type="text" name="userId"><br>
PW: <input type="password" name="password"><br>
<button type="submit">로그인</button>
</form>
loginProcess.jsp
<%
String userId = request.getParameter("userId");
String password = request.getParameter("password");
if ("jang".equals(userId) && "1234".equals(password)) {
session.setAttribute("userId", userId);
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("login.jsp?error=1");
}
%>
CREATE TABLE access_log (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(50),
action_type VARCHAR(20), -- login, logout 등
access_time DATETIME
);
JDBC 로그 저장 예제
<%
Connection conn = ... // DB 연결
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO access_log (user_id, action_type, access_time) VALUES (?, ?, NOW())"
);
pstmt.setString(1, userId);
pstmt.setString(2, "logout");
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
💡 다음에 해볼 수 있는 기능
기능 설명 로그인 유지 (자동 로그인) 쿠키 활용 브라우저별 동시 로그인 차단 같은 아이디 로그인 중복 방지 로그인 제한 로그인 5회 실패 시 10분 차단 관리자 전용 페이지 세션값이 "admin"인 경우만 접근 허용
💬 원하면 지금까지 기능 ZIP 구조로 JSP 프로젝트 템플릿으로 정리해줄게!
“로그인 + 로그아웃 + 로그 저장 + UI까지” 완성된 걸로 😊
추가하고 싶은 기능 또 있어? or 지금부터 하나씩 구현해볼까? 🚀