웹 프로그래밍 08(JSP)-쿠키를 다루어 보자
*Cookie(쿠키)를 다루어 보자
쿠키 - 서버에서 받은 정보를 유지하기 위해 클라이언트에 저장된 정보
※쿠키는 저장 용량 제한 있다.(보통 4kb)
-쿠키를 서버 만드는 방법
쿠키 객체를 생성 -> 객체에 대한 옵션 설정 -> response에 탑재
-쿠키 관련 메소드
*실전
-로그인 예제를 만들어 보자
main.html
logincheck.jsp
login.jsp
loginout.jsp
main.html : id와 pw를 입력 후 logincheck.jsp에 쿠키 전송
logincheck.jsp : id와 pw 대조 후 맞으면 login.jsp에 쿠키 전송, 틀리면 다시 main.html 페이지 이동
login.jsp : 로그인 화면
loginout.jsp : 쿠키를 삭제
-main.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <form action="logincheck.jsp" method="POST">
아이디 : <input type="text" name="id" size="10"><br /> 비밀번호 : <input type="password" name="pw" size="18"><br />
<input type="submit" value="로그인"> <input type="reset" value="초기화"> </form>
</body> </html> |
-logincheck.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<%@ 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, pw; %>
<% id = request.getParameter("id"); pw = request.getParameter("pw");
if(id.equals("abcd") && pw.equals("1234")) { Cookie cookie = new Cookie("id",id); cookie.setMaxAge(60*60); response.addCookie(cookie); response.sendRedirect("login.jsp"); } else response.sendRedirect("main.html");
%>
</body> </html> |
-login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<%@ 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(); for(int i=0 ; i<cookie.length ; i++) { String id = cookie[i].getValue(); if(id.equals("abcd")) out.println("abcd님 어서오세요!" + "<br />"); } %> <a href="logout.jsp">로그아웃</a>
</body> </html> |
-loginout.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<%@ 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> 로그아웃 되었습니다.<br />
<% Cookie[] cookie = request.getCookies();
out.println("현재쿠키목록"+"<br />"); for(int i=0;i<cookie.length;i++) { String value = cookie[i].getValue(); out.println(value+"<br />"); if(cookie[i].getValue().equals("abcd")) { cookie[i].setMaxAge(0); response.addCookie(cookie[i]); } }
%> <a href="main.html">메인으로</a> </body> </html> |
*출력결과
main.html
login.jsp
loginout.jsp
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 10(JSP)-예외처리를 해보자 (0) | 2020.01.03 |
---|---|
웹 프로그래밍 09(JSP)-세션을 다루어 보자 (0) | 2020.01.03 |
웹 프로그래밍 07(JSP)-액션태그, jsp:include (0) | 2020.01.03 |
웹 프로그래밍 06(JSP)-요청과 응답 (0) | 2020.01.02 |
웹 프로그래밍 05(JSP)-기본예제 (0) | 2020.01.02 |