Cookie是服務(wù)器存放在客戶端的一些數(shù)據(jù),比如密碼,以及你曾經(jīng)訪問(wèn)過(guò)的一些數(shù)據(jù)。
設(shè)置Cookie
復(fù)制代碼 代碼如下:
//設(shè)置cookie
Cookie cookie = new Cookie("TOM","111");
//設(shè)置有效期,默認(rèn)秒為單位
cookie. setMaxAge(7*24*60*60);
//添加cookie到客戶端
response.addCookie(cookie);
獲取Cookie
復(fù)制代碼 代碼如下:
%
//獲取cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null cookies.length>0) {
for(int i=0; icookies.length;i++) {
out.print(cookies[i].getName() +"————"+ cookies[i].getValue() + "br/>");
}
}
%>
Cookie的中文支持
Cookie不支持中文的顯示,必要的時(shí)候,需要進(jìn)行轉(zhuǎn)碼和解碼。
轉(zhuǎn)碼
復(fù)制代碼 代碼如下:
strings[j]=java.net.URLEncoder.encode(list.get(i-1)[j],"UTF-8");
解碼
復(fù)制代碼 代碼如下:
out.print(java.net.URLDecoder.decode(cookies[i].getValue(),"UTF-8") + "br/>");
動(dòng)態(tài)刪除Table當(dāng)前行
復(fù)制代碼 代碼如下:
script language="javascript">
function deletegoods(obj) {
var rowIndex = obj.parentNode.parentNode.rowIndex;
var table = document.getElementById("table");
table.deleteRow(rowIndex);
}
/script>
Cookie實(shí)現(xiàn)購(gòu)物車功能
以數(shù)組模擬選擇過(guò)程,并把所有的選擇項(xiàng)放入Arraylist中。
復(fù)制代碼 代碼如下:
%@page import="java.util.ArrayList"%>
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
!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>
script language="javascript">
function deletegoods(obj) {
var rowIndex = obj.parentNode.parentNode.rowIndex;
var table = document.getElementById("table");
table.deleteRow(rowIndex);
}
/script>
/head>
body>
p>
%
//設(shè)置cookie
ArrayListString[]> list = new ArrayListString[]>();
list.add(new String[]{"1001","img/1.jpg","經(jīng)典基本款低幫帆布女鞋2.0 粉綠色","#","39","¥69.00","1"});
list.add(new String[]{"1002","img/2.jpg","基礎(chǔ)羅紋Y字吊帶背心 奶藍(lán)色","#","m","¥9.00","1"});
int times = 1*24*60*60;
for(int i=1;i=list.size();i++){
String[] strings = new String[list.get(i-1).length];
for(int j=0;jlist.get(i-1).length;j++){
strings[j]=java.net.URLEncoder.encode(list.get(i-1)[j],"UTF-8");
}
Cookie cookie = new Cookie("item_id_"+i,strings[0]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_img_"+i,strings[1]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_title_"+i,strings[2]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_URL_"+i,strings[3]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_size_"+i,strings[4]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_price_"+i,strings[5]);
cookie.setMaxAge(times);
response.addCookie(cookie);
cookie = new Cookie("item_number_"+i,strings[6]);
cookie.setMaxAge(times);
response.addCookie(cookie);
}
%>
%
//獲取cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null cookies.length>0) {
for(int i=0; icookies.length;i++) {
out.print(cookies[i].getName() +"————"+ java.net.URLDecoder.decode(cookies[i].getValue(),"UTF-8") + "br/>");
}
}
if(cookies!=null cookies.length>5){
ArrayListString[]> goodslist = new ArrayListString[]>();
int i=1;
for(int j=0;jcookies.length/7;j++){
String[] strings = new String[7];
for(int k=0;kstrings.length;k++){
strings[k]=java.net.URLDecoder.decode(cookies[i++].getValue(),"UTF-8");
}
goodslist.add(strings);
}
pageContext.setAttribute("goodslist", goodslist);
}
// pageContext.setAttribute("length", cookies.length);
%>
/p>
form name="form1" method="post" action="">
table width="700" border="0" id="table">
tr>
td>ID/圖片/商品名/td>
td>SIZE/td>
td>單價(jià)/td>
td>數(shù)量/td>
td> /td>
/tr>
c:forEach items="${goodslist}" var="goods">
tr id="${goods[0]}">
td>${goods[0]}img src="${goods[1]}" border="0"/>/a>a target="_blank"
title="${goods[2]}" href="${goods[3]}">${goods[2]}/a>/td>
td>${goods[4]}/td>
td>${goods[5]}/td>
td>${goods[6]}/td>
td>a href="#">收藏/a> a href="#" onclick="deletegoods(this)">刪除/a>/td>
/tr>
/c:forEach>
/table>
/form>
p> /p>
/body>
/html>
購(gòu)物車實(shí)現(xiàn)結(jié)果
上面部分是獲取的Cookie。
下面的部分是將Cookie通過(guò)布局,放入購(gòu)物車頁(yè)面。
點(diǎn)擊刪除,將會(huì)動(dòng)態(tài)刪除當(dāng)前行。
您可能感興趣的文章:- Javascript操縱Cookie實(shí)現(xiàn)購(gòu)物車程序
- 登錄時(shí)記住用戶名和密碼及cookie案例應(yīng)用
- js讀寫COOKIE實(shí)現(xiàn)記住帳號(hào)或密碼的代碼(js讀寫COOKIE)