第一次使用cookie
給招行做的一個(gè)小游戲,有個(gè)需求是:分?jǐn)?shù)第一次達(dá)到500會(huì)彈出一個(gè)提示框。之前沒有使用過cookie,在網(wǎng)上搜了些資料,最終還是順利實(shí)現(xiàn)了功能。在這里貼一下代碼。
?
<%!
//初始化cookie
public String getCookie(HttpServletRequest request, String cookieName) {
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(cookieName)) {
return cookie.getValue();
}
}
}
return "";
}
public void setCookie(HttpServletResponse response, String cookieName, String cookieValue, int maxAge) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(maxAge >= 0 ? maxAge : -1);
response.addCookie(cookie);
}
%>
<%
//獲取當(dāng)前用戶的總分
int totalScore = player.getScore(self.getOpenid());
try {
// 獲取分?jǐn)?shù)
totalScore = Integer.parseInt(request.getParameter("totalScore"));
} catch (Exception e) {
}
boolean showDialog = false;
if (totalScore >= 500) {
String isDialogShowed = getCookie(request, "isDialogShowed");
if (!"true".equals(isDialogShowed)) {
// 第一次達(dá)到500分
setCookie(response, "isDialogShowed", "true", 86400);
showDialog=true;
}
}
%>
轉(zhuǎn)載于:https://www.cnblogs.com/will-2008/p/4898229.html
總結(jié)
以上是生活随笔為你收集整理的第一次使用cookie的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玉米秸秆多少钱一吨
- 下一篇: 02_计算机科学和软件工程的区别