Use and save Chinese characters of cookies and use cookies to implement the shopping cart Function

Source: Internet
Author: User

Cookies are some data that the server stores on the client, such as passwords and some data you have accessed.

Set Cookie
Copy codeThe Code is as follows:
// Set cookie
Cookie cookie = new Cookie ("TOM", "111 ");
// Set the validity period. The default unit is seconds.
Cookie. setMaxAge (7*24*60*60 );
// Add the cookie to the client
Response. addCookie (cookie );

Get Cookie
Copy codeThe Code is as follows:
<%
// Obtain the cookie
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 characters. If necessary, they must be transcoded and decoded.

Transcoding
Copy codeThe Code is as follows:
Strings [j] = java.net. URLEncoder. encode (list. get (I-1) [j], "UTF-8 ");

Decoding
Copy codeThe Code is as follows:
Out. print (java.net. URLDecoder. decode (cookies [I]. getValue (), "UTF-8") + "<br/> ");

Dynamically Delete the current Table row
Copy codeThe Code is as follows:
<Script language = "javascript">
Function deletegoods (obj ){
Var rowIndex = obj. parentNode. parentNode. rowIndex;
Var table = document. getElementById ("table ");
Table. deleteRow (rowIndex );
}
</Script>

Cookie to implement the shopping cart Function

Simulate the selection process with arrays, and put all the selection items into the Arraylist.
Copy codeThe Code is 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">
<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>
<%
// Set cookie
ArrayList <String []> list = new ArrayList <String []> ();
List. add (new String [] {"1001", "img/1.jpg"," classic basic low-help canvas women's shoes 2.0 pink green "," # "," 39 "," ¥ 69. 00 "," 1 "});
List. add (new String [] {"1002", "img/2.jpg"," Basic rib Y-shaped camisole 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 );
}
%>
<%
// Obtain the cookie
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 = "700" border = "0" id = "table">
<Tr>
<Td> ID/image/product name </td>
<Td> SIZE </td>
<Td> unit 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>
</Html>

Shopping Cart implementation result
 
The above section lists the obtained cookies.

The following section uses the Cookie layout to go to the shopping cart page.

Click Delete to delete the current row dynamically.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.