Get cookie value in jsp

read value

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String cookieName = "username";
Cookie[] cookies = request.getCookies();

if (cookies != null) {
 for (Cookie cookie : cookies) {
   if (cookie.getName().equals(cookieName)) {
     //do something
     //value can be retrieved using #cookie.getValue()
    }
  }
}
%>

set value

Cookie cookie = new Cookie ("username",username);
response.addCookie(cookie);

delete value

Cookie cookie = new Cookie ("username",username);
cookie.setMaxAge(0);
response.addCookie(cookie);

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *