Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 예외처리
- JSP
- HTML
- Android
- 메소드
- oracle
- AWT
- layout
- struts2
- Eclips
- mybatis
- Graphic
- 배열
- 전화걸기
- 이클립스
- 클래스
- paint
- JavaScript
- 메서드
- 생성자
- Menu
- 에러페이지
- Spring
- 오버로딩
- OGNL
- 기본
- 국제화
- Java
- 안드로이드
- 어노테이션
Archives
- Today
- Total
note
JSP 쿠키 생성,보기,삭제,수정 본문
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.net.URLEncoder" %>
<%
Cookie cookie = new Cookie("name", URLEncoder.encode("최범균","utf-8"));
//쿠키 유효시간 지정
//쿠키 유효시간을 지정하지 않으면 정보를 메모리에 저장하고
//쿠키 유효시간을 지정하면 정보를 파일에 저장
//cookie.setMaxAge(-1) : 정보를 메모리에 저장
//cookie.setMaxAge(0) : 쿠키 유효시간 만료
cookie.setMaxAge(30*60);
//생성된 쿠키를 클라이언트에 전송
response.addCookie(cookie);
%>
<!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=UTF-8">
<title>쿠키 생성</title>
</head>
<body>
<%= cookie.getName() %>쿠키의 값 = "<%=cookie.getValue() %>"
</body>
</html>
쿠키 보기
한번 실행시
두번 실행시
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder"%>
<!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=UTF-8">
<title>쿠키 목록</title>
</head>
<body>
쿠키 목록
<br>
<%
//클라이언트가 서버에 제공한 쿠키 정보를 Cookie[]로 반환
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
%>
<%=cookies[i].getName()%>
=<%=URLDecoder.decode(cookies[i].getValue(), "utf-8")%><br>
<%
}
} else {
%>
쿠키가 존재하지 않습니다.
<%
}
%>
</body>
</html>
수정,삭제
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.net.URLEncoder" %>
<%
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length >0){
for(int i = 0; i<cookies.length; i++){
if(cookies[i].getName().equals("name")){
Cookie cookie = new Cookie("name",URLEncoder.encode("JSP프로그래밍","utf-8"));
cookie.setMaxAge(30*60);
response.addCookie(cookie);
}
}
}
%>
<!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=UTF-8">
<title>값 변경</title>
</head>
<body>
name 쿠키의 값을 변경합니다.
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.net.URLEncoder" %>
<%
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length >0){
for(int i = 0; i<cookies.length; i++){
if(cookies[i].getName().equals("name")){
Cookie cookie = new Cookie("name","");
//클라이언트가 쿠키명이 name인 쿠키를 가지고 있을 경우
//해당 쿠키 정보가 만료됨
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
%>
<!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=UTF-8">
<title>쿠키 삭제</title>
</head>
<body>
name 쿠키를 삭제 합니다.
</body>
</html>
쿠키 경로 지정
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder" %>
<%
Cookie cookie1 = new Cookie("path1",URLEncoder.encode("경로:/chap09/path1","utf-8"));
cookie1.setPath("/chap09/path1");
cookie1.setMaxAge(30*60);
response.addCookie(cookie1);
Cookie cookie2 = new Cookie("path2",URLEncoder.encode("경로:","utf-8"));
cookie2.setMaxAge(30*60);
response.addCookie(cookie2);
Cookie cookie3 = new Cookie("path3",URLEncoder.encode("경로:/","utf-8"));
cookie3.setPath("/");
cookie3.setMaxAge(30*60);
response.addCookie(cookie3);
Cookie cookie4 = new Cookie("path4",URLEncoder.encode("경로:/chap09/path2","utf-8"));
cookie4.setPath("/chap09/path2");
cookie4.setMaxAge(30*60);
response.addCookie(cookie4);
%>
<!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=UTF-8">
<title>쿠키 경로 지정</title>
</head>
<body>
다음 과 같이 쿠키를 생성했습니다<br>
<%= cookie1.getName() %>=<%=cookie1.getValue() %>
[<%=cookie1.getPath() %>]
<br>
<%= cookie2.getName() %>=<%=cookie2.getValue() %>
[<%=cookie2.getPath() %>]
<br>
<%= cookie3.getName() %>=<%=cookie3.getValue() %>
[<%=cookie3.getPath() %>]
<br>
<%= cookie4.getName() %>=<%=cookie4.getValue() %>
[<%=cookie4.getPath() %>]
<br>
</body>
</html>
'JSP > 기본' 카테고리의 다른 글
| JSP 자바빈 사용하기(자동 생성방법) , 액션태그,getProperty,setProperty 사용 (0) | 2012.02.08 |
|---|---|
| JSP 세션 Session (0) | 2012.02.07 |
| JSP 에러페이지 만들기 (0) | 2012.02.07 |
| JSP 페이지 이동 (1) | 2012.02.07 |
| JSP include (0) | 2012.02.07 |