Gentle Breeze

[jsp/Servlet] Cookie 본문

⑥ PreSTC/jsp / Servlet

[jsp/Servlet] Cookie

재령 2008. 7. 24. 10:08
* makeCookie.jsp

<%@ page contentType="text/html; charset=euc-kr" %>
<html>
<head>
 <title>쿠키를 생성하는 예제</title>
</head>
<%
        String cookieName = "id";
        Cookie cookie = new Cookie(cookieName, "honghong");
        cookie.setMaxAge(60*2); //초
        //cookie.setValue("zephyr"); // 쿠키값 재설정하는 부분.
        response.addCookie(cookie);
%>

<body>
<h2>쿠키를 생성하는 예제</h2>
<P>
"<%=cookieName%>"
쿠키가생성 되었습니다.<br>
<input type="button" value="쿠키의 내용확인" onclick="javascript:window.location='useCookie.jsp'">
</P>
</body>
</html>

===============================================================================

* useCookie.jsp

<%@ page contentType="text/html; charset=euc-kr" %>
<html>
<head>
 <title>웹 브라우저에 저장된 쿠키를 가져오는 예제</title>
</head>
<body>
<h2>웹 브라우저에 저장된 쿠키를 가져오는 예제</h2>
<%
      Cookie[] cookies = request.getCookies();
      if(cookies!=null){
           for(int i=0; i<cookies.length;++i){
                  if(cookies[i].getName().equals("id")){
%>
    쿠키의 이름은 "<%=cookies[i].getName()%>" 이고
    쿠키의 값 "<%=cookies[i].getValue()%>" 입니다.
<%
                  }
            }
      }
%>
</body>
</html>

'⑥ PreSTC > jsp / Servlet' 카테고리의 다른 글

[jsp/Servlet] Session  (0) 2008.07.24
[jsp/Servlet] Session / Cookie  (0) 2008.07.24
[jsp/Servlet] Error page  (0) 2008.07.24
[jsp/Servlet] MVC (Model View Controller) Model  (0) 2008.07.24
[Servlet] web.xml <3>  (0) 2008.07.23
Comments