2013년 7월 12일 금요일

(130712) 15일차 CookieLogin.jsp, CookieLoginProc.jsp, CookieLogin1.jsp (JSP Cookie를 이용한 Login)

 - CookieLogin.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 = "CookieLoginProc.jsp" method = "post">
아이디 : <input type = "text" name = "id"> <br>
패스워드 : <input type = "text" name = "pass"> <br>
<input type = "submit" value = "전송">
</form>
</body>
</html>


 - CookieLoginProc.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)){
// 클라이언트에 쿠키값이 저장되고 쿠키 이름은 cookie1이고 값은 enter1129
response.addCookie(new Cookie("cookie1", id));
response.sendRedirect("CookieLogin1.jsp");
}
else{
%>
비밀번호를 다시 확인해주십시오.
<%
}
%>
</body>
</html>


 - CookieLogin1.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>
<%
Cookie [] cookie = request.getCookies(); // 가져올 때는 한개만 가져오는 것이 불가능하다. 무조건 다 가져온다.

if(cookie != null && cookie.length > 0){
for(int i = 0 ; i < cookie.length ; i++){
if(cookie[i].getValue().equals("enter1129")){
%>
<%=cookie[i].getValue()%>님이 로그인 되었습니다.
<% }
}
}

%>
</body>
</html>


 - 결과