2013년 7월 12일 금요일

(130712) 15일차 SessionLogin.jsp, SessionLoginProc.jsp, SessionLogin1.jsp, Login.jsp (JSP Session을 이용한 Login)

 - Session 
 : Client가 무언가를 임시로 저장하고 싶을때 사용한다. 
 : 여러개의 페이지에서 공통으로 사용해야 될 정보가 있을때 저장해서 사용한다.
 : 즉, 모든 페이지에서 동일한 정보를 가져다 쓸 수 있도록 하기 위해 사용한다.
 : Session은 Map 계열 (Key와 Value 한 쌍으로 이루어짐)
 : Cookie는 Client에 정보가 저장되고, Session은 Server에 정보가 저장되는 차이점이 있다.



 - SessionLogin.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action = "SessionLoginProc.jsp" method = "post">
아이디 : <input type = "text" name = "id"> <br>
패스워드 : <input type = "text" name = "pass"> <br>
<input type = "submit" value = "전송">
</form>
</body>
</html>


 - SessionLoginProc.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id1 = "enter1129";
String pass1 = "12345";
String id = request.getParameter("id");
String pass = request.getParameter("pass");
if(id.equals(id1) && pass.equals(pass1)){
// Session은 Map 계열이므로 한 쌍의 Key와 Value로 이루어짐 - id1을 키값으로 받음
session.setAttribute("name", id1);
session.setAttribute("pass", pass1);
// 세션 유지시간을 부여
session.setMaxInactiveInterval(60);
response.sendRedirect("SessionLogin1.jsp");
}
else{
%>
<script type="text/javascript">
alert("아이디와 비밀번호를 다시 확인해 주십시오.");
history.go(-1); // -1은 이전페이지로 이동하라는 의미
</script>
<%
}
%>
</body>
</html>


 - SessionLogin1.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id = session.getAttribute("name").toString();
String pass = session.getAttribute("pass").toString();
%>
당신의 이름은 <%=id%> 이고 비밀번호는 <%=pass%> 입니다. <br><br>
<a href = "Login.jsp"> 로그인 페이지로 이동 </a>
</body>
</html>


 - Login.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form>
아이디 : <input type = "text" name = "id" value = "<%=session.getAttribute("name")%>" > <br>
패스워드 : <input type = "text" name = "pass" value = "<%=session.getAttribute("pass")%>" > <br>
<input type = "submit" value = "전송">
</form>
</body>
</html>


 - 결과