Java Web Development _ Shopping Cart function realizes----cookie,session and so on many kinds of realization way

Source: Internet
Author: User
Tags java web
Java Web Development _ Shopping Cart function Realization

No previous contact with the shopping cart Dongdong, also do not know how to do shopping cart, so in the query a lot of information, summed up the function of the shopping cart to achieve.

For information, find three ways to:

1. Use cookies to realize the shopping cart;

2. Implement the shopping cart with session;

3. Use cookies and database (shopping cart information persistence) to achieve the shopping cart;

=========================================================================

Analyze the pros and cons of these three approaches:

1. Simple cookies to implement a shopping cart, such a shopping cart is not ideal, imagine if the client's browser to disable the cookie,

This way it's going to miscarry here ...

2.session Save the shopping cart information, this is only available in a session, if the user is not logged in, or said to log in after the Add shopping cart, in the closing browser

or log out, before the addition of the shopping cart are all aborted ...

3. I would say this is the way ...

The main process:

A. User log in before the data flow: Users do not login to the system, the favorite items to add shopping cart, then this time, we can store the shopping cart information

To cookies, this involves adding cookies, modifying the operation, or adding the cookie if the cookie is not stored in the cookie before.

If there is a cookie in the cookie, then it is time to modify the cookie (this involves the user adding a cart to the same item multiple times).

B. User logon Data flow: After the user login, the system first thing to do is to obtain the corresponding cookies, if there is a relevant shopping cart cookies, then the cart

The information is persisted by the corresponding user, either added or modified. (Add action: This user's corresponding shopping cart if there is no corresponding information to add action; Modify operation: similar,

If there is a corresponding user's shopping cart information, modify the operation. When a user logs in, the shopping cart can also be added, but this is not added to the cookie, but is persisted directly to the

Database. Note: The data that the user logs in to deal with the database.

=========================================================================

Code section:

=========================================================================

Note:

1 conf.iduona_cashticket_cookie_startname = "iduona_cashticket_";

  1/** 2 * User Login 3 * 4 * @author hongten 5 */6 public void Login () {7/user Login
  When recording, to read cookies, and the operation of persistent words, more login operation here omitted ....
  8 Peristshoppingcartwhenuserlogin (NewUser);      9} 10 11/** 12 * Add Shopping cart <br> * ============================================<br> 14 * User Login before:<br> 15 * Users in the selection of cash coupons, click on the cash coupons to add to the cart, the cash coupon information (cash voucher ID, purchase quantity) <br> 16 * Delivered to here, this time, backstage to do is Whether the same record is queried from the cookie, if there is the same record <br> 17 * then update the corresponding record; otherwise, add a new record <br> 18 * User Login after:<br> 19 * with When you log in, if you add a shopping cart operation, you do not need to save it to a cookie, but you can directly persist the cart information <br> * @throws Exception Public void AddToShoppingCart () throws Exception {if (Cashticket = = NULL | | Cashticket.getid () = NULL | | cashticket
 . GetId () < 1) {write ("Nullid"); } else if (q = = NULL | | q = = "") {27//purchase quantity, by default 1 q = string.valueof (1); /else {30//Read all cookies to cookie cookies[] = servletactioncontext.getrequest (). GE
 Tcookies (); if (cookies = = NULL | | Cookies.length < 0) {33//No cookie System.
 Out.println ("There is no cookie ...");                     /else {36//Determine whether the user is logged in to the getuserinsession () = = null) {38
 Boolean flag = true; The C:cookies (Cookie) {c.getname (). Equals (Conf.iduona_cashticke                             T_cookie_startname + Cashticket.getid ()) {41//Description Existing cookies have corresponding cookies, update operation 42
 Integer OldValue = integer.valueof (C.getvalue ());
 The Integer newvalue = integer.valueof (OldValue + integer.valueof (q));
 Fixcookie (c, newvalue.tostring (). Trim ());
 Flag = false;    46                     {47} 48//Description Existing Cookies do not have a corresponding cookie, add Operation 49  if (flag) {Addcookie (Conf.iduona_cashticket_cookie_startname + cashticket.getid (),
 Q.trim ());                     51} 52 53//================================================== 54
 For testing, read all Cookies Readshoppingcartfromcookie (); A//================================================== write ("Success")
 ; /else {60///If the user is logged in, stating that session exists user, then the persistent shopping cart information Cashtick
 ET cashtickettemp = Cashticketservice.get (Cashticket.getid ());                         if (Shoppingcartservice.isexistuserandcashticket (Getuserinsession (), cashtickettemp)) {63 ShoppingCart Oldshoppingcart = Shoppingcartservice.getbyuserandcashticket (getuserinsesSion (), cashtickettemp);
 Oldshoppingcart.setamount (Oldshoppingcart.getamount () + integer.valueof (q)); (Shoppingcartservice.update (Oldshoppingcart)) {Write ("Succe
 SS "); Shoppingcarttemp} else {ShoppingCart =
 New ShoppingCart ();
 Shoppingcarttemp.setamount (integer.valueof (q));
 Shoppingcarttemp.setuser (Getuserinsession ());
 Shoppingcarttemp.setcashticket (cashtickettemp);
 Shoppingcarttemp.setcreatetime (New Date ());
 Shoppingcarttemp.setstatustype (statustype.positive);
 Shoppingcarttemp.setuuid (Uuid.randomuuid (). toString ()); The IF (Shoppingcartservice.save (shoppingcarttemp)) {Write ("succes
 S ");    78                     } 79} 80} 81} 82} 83} 84 85  /** 86 * Read shopping cart information from cookies * @throws Exception * @return Readshoppingcartfromcookie () throws Exception {System.out.println ("=========================================
 =============");
 cookies[Cookie] = Servletactioncontext.getrequest (). GetCookies (); if (cookies = null | | Cookies.length < 0) {//System.out.println ("There is no cookie.")
 ."); 96//No Cookie c:cookies} else {The for (cookie) {System.out.
println ("haha there are many cookies:" + c.getname () + "" + C.getvalue ());      100} 101} 102} 103 104/** 105 * Add Cookie Operation # * @param name 108
 * Name of Cookie

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.