728x90
로그인 페이지 3_login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 로그인 성공 시 생성된 쿠키 가져옴
Cookie[] cookies = request.getCookies();
String userid = null;
if (cookies != null) { // 쿠키가 있으면 userid 값 세팅
for (Cookie cookie : cookies) {
if ("userid".equals(cookie.getName())) {
userid = cookie.getValue(); // userid
}
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>로그인</h2>
<%
if (userid == null) {
%>
<form method="post" action="3_login_ok.jsp">
<p><input type="text" name="userid"></p>
<p><input type="password" name="userpw"></p>
<p><input type="submit" value="로그인"></p>
</form>
<%
} else {
%>
<h3><%=userid %>님 환영합니다!</h3>
<p><input type="button" value="로그아웃" onclick="location.href='3_logout.jsp'"></p>
<%
}
%>
</body>
</html>
아이디 비밀번호 확인 3_login_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String userid = request.getParameter("userid");
String userpw = request.getParameter("userpw");
// admin / 1234
// id password 일치 시 쿠키 생성
if (userid.equals("admin") && userpw.equals("1234")) {
Cookie cookie = new Cookie("userid", userid);
cookie.setMaxAge(60 * 20); // 20
response.addCookie(cookie);
%>
<script>
alert("로그인 되었습니다.");
location.href = "3_login.jsp";
</script>
<%
} else {
%>
<script>
alert("아이디 또는 비밀번호를 다시 확인하세요.");
history.back();
</script>
<%
}
%>
로그아웃 페이지 3_logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("userid".equals(cookie.getName())) { // 쿠키가 존재하면 쿠키를 수정
Cookie newCookie = new Cookie("userid", ""); // userid를 없앰
newCookie.setMaxAge(0); // 쿠키 유지 안되게 설정
response.addCookie(newCookie);
}
}
}
%>
<script>
alert("로그아웃 되었습니다.");
location.href="3_login.jsp";
</script>
728x90
'JSP' 카테고리의 다른 글
[JSP] session 예제 (장바구니) (0) | 2022.05.23 |
---|---|
[JSP] session 이용한 로그인 (0) | 2022.05.23 |
[JSP] JSP의 내장 객체 (0) | 2022.05.23 |
[JSP] 회원가입 (0) | 2022.05.10 |
[JSP] HTTP method(GET, POST) (0) | 2022.05.10 |