Notice
Recent Posts
Recent Comments
Link
Everything has an expiration date
[JSP & JDBC & Oracle] 20231215 - [프로그램 소스] - TestSession01, TestSession01_ok, Logout 본문
[JSP & JDBC & Oracle]/Program source (JSP & JDBC & Oracle)
[JSP & JDBC & Oracle] 20231215 - [프로그램 소스] - TestSession01, TestSession01_ok, Logout
Jelly-fish 2023. 12. 18. 01:24
WebApp13
012
TestSession01.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// 최초 요청일 경우... 아무 것도 없겠지만...
// TestSession01_ok.jsp 페이지에서
// 로그인 성공 후에 요청된 페이지라면...
// session 의 userId 에 superman 을...
// userName 에 문정환을 담아서 보낸 상황
String userId = (String)session.getAttribute("userId"); // "superman"
String userName = (String)session.getAttribute("userName"); // "문정환"
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//-- 『session.getAttribute("userId");』는
// Object 타입을 반환하므로
// String 타입으로 변환하는 과정 필요(다운 캐스팅)
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// 추가~!!!
// 세션 활성화 시간 변경 -----------------------------------------------------
// ※ 세션 기본(default) 시간은 1800초.
session.setMaxInactiveInterval(10);
//-- 세션이 유지되는 시간을 10초로 설정한 상태.
// 이로 인해...
// 로그인 후 10초 동안 아무 액션도 없는 상태에서...
// 다시 기능을 이용하려 하거나, 페이지 새로고침을 수행하면
// 로그아웃 처리된 것을 확인할 수 있다.
// ----------------------------------------------------- 세션 활성화 시간 변경
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestSession01.jsp</title>
<link rel="stylesheet" type="text/css" href="css/MemberScore.css">
<style type="text/css">
a {text-decoration: none;}
.btnMenu
{
border: 1px solid gray;
border-radius: 3px;
font-size: 8pt;
width: 60px;
height: 20px;
}
</style>
<script type="text/javascript">
function sendIt()
{
// 확인
//alert("함수 호출~!!!");
var f = document.myForm;
// form 객체에 . 을 찍고 name을 적어주면
// ID 없이도 접근 가능하다.
if (!f.userId.value)
{
alert("아이디를 입력해야 합니다~!!!");
f.userId.focus();
return;
}
if (!f.userPwd.value)
{
alert("패스워드를 입력해야 합니다~!!!");
f.userPwd.focus();
return;
}
// 위의 if문을 모두 통과했다면
// 아이디, 패스워드 모두 입력한 것이므로...
f.submit();
}
</script>
</head>
<body>
<div>
<h1>세션 처리 - 로그인</h1>
<hr>
</div>
<div>
<table>
<tr>
<td>
<a href="">
<button type="button" class="btnMenu btn01">게시판</button>
</a> |
<%
// userId가 null일 때 로그인을 하지 않은 상태이므로 버튼 비활성화.
// 브라우저 창 전부를 다 꺼야!!!!!!!
// 제대로 Session이 제거된다~!!!
if(userId==null)
{
%>
<a href="">
<button type="button" class="btnMenu btn02">일정관리</button>
</a> |
<a href="">
<button type="button" class="btnMenu btn02">친구관리</button>
</a> | ...
<%
}
else
{
%>
<a href="Sca.jsp">
<button type="button" class="btnMenu btn01">일정관리</button>
</a> |
<a href="Fri.jsp">
<button type="button" class="btnMenu btn01">친구관리</button>
</a> | ...
<%
}
%>
</td>
</tr>
</table>
<br><br>
<table>
<tr>
<td>
<%
if (userId==null)
{
%>
<form action="TestSession01_ok.jsp" method="post" name="myForm">
<table>
<tr>
<th>아이디</th>
<td>
<input type="text" name="userId" class="txt">
</td>
</tr>
<tr>
<th>패스워드</th>
<td>
<!-- <input type="password"> -->
<!-- 값을 눈으로 확인할 수 있도록 "text"로 구성 -->
<input type="text" name="userPwd" class="txt">
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" class="btn" style="width: 100%;"
onclick="sendIt()">로그인</button>
</td>
</tr>
</table>
</form>
<%
}
else
{
%>
<h2><%=userName %>(<%=userId %>)님, 환영합니다.</h2>
<p>이제, 일정관리와 친구관리 서비스를 이용할 수 있습니다.</p>
<p>
<a href="Logout.jsp">
<button type="button" class="btnMenu btn01">로그아웃</button>
</a>
</p>
<%
}
%>
</td>
</tr>
</table>
</div>
</body>
</html>
TestSession01_ok.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// TestSession01_ok.jsp
// 이전 페이지(TestSession01.jsp)로부터 넘어온 데이터 수신
// → userId, userPwd
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
// DB 활용 → dao 로그인 처리 → dto 활용
// → 로그인 관련 테이블의 데이터와 비교 → 최종적으로 로그인 액션 처리
String memberId = "superman";
String memberPwd = "1234567";
String memberName = "문정환";
if (userId.equals(memberId) && userPwd.equals(memberPwd))
{
// 로그인 액션 처리
session.setAttribute("userId", userId); //-- session 의 userId → superman
session.setAttribute("userName", memberName); //-- session 의 userName → 문정환
// 클라이언트가 페이지를 다시 요청할 수 있도록 안내
response.sendRedirect("TestSession01.jsp");
}
//-------------------------------------------------------
// if의 조건이 false 라면... (로그인이 잘못 되었다면)
// 클라이언트는 이 페이지에 머물 것이다.
//-------------------------------------------------------
//----------------------------------------------------------------------------
// if의 조건이 true 라면... (데이터베이스에 사용자가 입력한 id, pwd가 일치)
// TestSession01.jsp로 돌아간다. → TestSession01.jsp 페이지를 다시 요청한다.
//----------------------------------------------------------------------------
// 로그인에 성공했다면, 보이는 것에서는 빈화면으로 똑같겠지만
// 세션에는 로그인 정보가 기록된 상태일 것이다!
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestSession01_ok.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>로그인 실패~!!!</h1>
<hr>
</div>
<a href="TestSession01.jsp">로그인 페이지로 돌아가기</a>
</body>
</html>
Logout.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// Logout.jsp
//-- 세션의 내용을 없애 로그아웃 처리
// 이후... 클라이언트가 다시 요청할 페이지 안내
// 이 페이지가 요청됐다면(Logout) session 지우기!
// 해당 세션 값을 제거하겠다는 의미
// 【하나씩 골라서 세션 값 삭제.】▤▤▤▤▤▤▤▤▤▤
session.removeAttribute("userId");
session.removeAttribute("userName");
//-- 세션의 사용자아이디와 이름 삭제
// ▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤
//-->> ex) 최종적으로 로그인한 날짜, 작성 게시글 수...
// 기존 세션에 저장되어 있는 모든 항목을 제거하고 세션을 초기화
session.invalidate();
//-- 세션의 모든 내용을 털어내서 삭제하는 메소드
// 클라이언트에게 다시 로그인 페이지를 요청할 수 있도록 안내
response.sendRedirect("TestSession01.jsp");
// ※ NOTICE ※====================================
// ★★★★★★★★★★★★★★★★★★★★★
// ★ 20240219 - FinalProject 정식 시작일 ★
// ★★★★★★★★★★★★★★★★★★★★★
// 남은 프로젝트는 파이널 프로젝트.
// 자리 안 바꾸고... 세미 안 함.
// 부족한 부분 보강.
// ================================================
%>