Cookies are data that the server stores on the client, such as passwords, and some data that you have visited.
Set Cookies
Copy Code code as follows:
Set cookies
Cookie cookie = new Cookie ("TOM", "111");
Set validity period, default second is unit
Cookie. Setmaxage (7*24*60*60);
Add Cookies to Client
Response.addcookie (cookie);
Get Cookies
Copy Code code as follows:
<%
Get cookies
cookie[] cookies = request.getcookies ();
if (cookies!=null && cookies.length>0) {
for (int i=0; i<cookies.length;i++) {
Out.print (Cookies[i].getname () + "————" + cookies[i].getvalue () + "<br/>");
}
}
%>
Chinese support for Cookies
Cookies do not support Chinese display, and when necessary, transcoding and decoding are required.
Transfer Code
Copy Code code as follows:
Strings[j]=java.net.urlencoder.encode (List.get (I-1) [j], "UTF-8");
decoding
Copy Code code as follows:
Out.print (Java.net.URLDecoder.decode (Cookies[i].getvalue (), "UTF-8") + "<br/>");
Dynamic Delete Table current row
Copy Code code as follows:
<script language= "JavaScript" >
function Deletegoods (obj) {
var rowIndex = Obj.parentNode.parentNode.rowIndex;
var table = document.getElementById ("table");
Table.deleterow (RowIndex);
}
</script>
cookies Implement shopping cart function
Simulates the selection process with an array and puts all the options in the ArrayList.
Copy Code code as follows:
<% @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" >
<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>
<body>
<p>
<%
Set cookies
arraylist<string[]> list = new arraylist<string[]> ();
List.add (New string[]{"1001", "Img/1.jpg", "Classic basic section low help canvas shoes 2.0 pink Green", "#", "39", "¥69.00", "1"});
List.add (New string[]{"1002", "img/2.jpg", "base rib y word strap vest milk blue", "#", "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;j<list.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);
}
%>
<%
Get cookies
cookie[] cookies = request.getcookies ();
if (cookies!=null && cookies.length>0) {
for (int i=0; i<cookies.length;i++) {
Out.print (Cookies[i].getname () + "————" + Java.net.URLDecoder.decode (Cookies[i].getvalue (), "UTF-8") + "<br/>") ;
}
}
if (cookies!=null && cookies.length>5) {
arraylist<string[]> goodslist = new arraylist<string[]> ();
int i=1;
for (int j=0;j<cookies.length/7;j++) {
String[] strings = new STRING[7];
for (int k=0;k<strings.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= "border=" "0" id= "table" >
<tr>
<td>id/Picture/Product name </td>
<td>SIZE</td>
<td> Price </td>
<td> Quantity </td>
<td> </td>
</tr>
<c:foreach items= "${goodslist}" var= "goods" >
<tr id= "${goods[0]}" >
<td>${goods[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= "#" > Favorites </a> <a href= "#" onclick= "Deletegoods (This)" > Delete </a></td>
</tr>
</c:forEach>
</table>
</form>
<p> </p>
</body>
Shopping Cart Implementation results
The above section is the cookie obtained.
The following section is to put the cookie through the layout, into the shopping cart page.
Clicking Delete will delete the current row dynamically.