web

[jsp] cookie 처리(생성, 읽기, 삭제) + 자바스크립트로!

소댓 2023. 4. 30. 15:14

< 텍스트 입력하고 쿠키와 함께 출력 >


- [userInput.jsp] : 입력창 만들기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>userInput.jsp</title>
</head>
<body>
    <form action="userInputOk.jsp">
        <input type="text" name="txt" id="" />
        <input type="submit" value="저장" />
    </form>
    
</body>
</html>
cs

 


- [userInputOk.jsp] : 쿠키 저장

  > 입력 내용 출력 + userCookieList.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=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>userInputOk.jsp</title>
</head>
<body>
    <%
        // Cookie
        // 서버가 사용자 브라우저에 남기는 작은 정보의 조각
        
        String txt = request.getParameter("txt");
        out.println("<h2>" + txt + "</h2>");    
        
        Cookie c = new Cookie("txt", txt);
        
        // 이 쿠키의 유통기한 1년
        // c.setMaxAge(60*60*24*365);
        c.setMaxAge(60*1); // 테스트를 위해서 유통기한 1분
        
        // 사용자 브라우저에 저장
        response.addCookie(c);
        
        
    %>
    <a href="userCookieList.jsp">쿠키보러가기</a>
    
</body>
</html>
cs

 


- [userCookieList.jsp] : 쿠키 관련 정보 출력

  > 쿠키 관련 정보는 '개발자 도구 - 애플리케이션 - 쿠키'에서 확인 가능

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>userCookieList.jsp</title>
</head>
<body>
    <h1>쿠키</h1>
    
    <%
        /* 요청 객체에서 쿠키 */
        Cookie[] clist = request.getCookies();
 
        for(Cookie c : clist){
            out.println("<h2> 쿠키 : " + c.getName() + " : " + c.getValue() + "</h2>");
        }
    %>
    
</body>
</html>
cs

 

 

 

 

 

< 자바스크립트로 쿠키 생성 및 처리 >


- [cookie.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookie.jsp</title>
<script type="text/javascript">
    /* 자바 스크립트로 쿠키 만들기 */
    window.onload = function(){
        document.cookie="name=coffee";
        var date = new Date();
        date.setTime(date.getTime()+ 1*60*1000); // 60초
        document.cookie="product=moniter;expires="+date.toUTCString();
        
        var x = document.cookie;
        console.log(x);
    }
 
</script>
</head>
<body>
 
</body>
</html>
cs

 


- [cookie2.jsp] : jquery에서 쿠키 처리(생성, 읽기, 삭제)  

>> dev에 jquery.cookie 압축 풀고 webapp-js 폴더 생성해서 복사!!
> jquery cdn 추가(snippets)

 

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
32
33
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookie2.jsp</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.cookie.js"></script>
<script type="text/javascript">
    $(function(){
        // 쿠키 생성 : 세션이 종료될 때까지 존재
        $.cookie("id""hong");
        
        // 7일 뒤에 만료되는 쿠키 생성
        $.cookie("keyword""coffee", {expires:7})
        
        // 쿠키 읽기
        console.log("keyword : "+$.cookie("keyword"));
        
        // 모든 쿠키 읽기 : 객체 형태로 리턴
        console.log($.cookie());
        
        // 쿠키 삭제 : true, false 리턴
        console.log("keyword 쿠키 삭제 : " + $.removeCookie("keyword"));
        
    })
</script>
</head>
<body>
 
</body>
</html>
cs