Detailed description of the shopping cart program compiled using the JSP Session mechanism

Source: Internet
Author: User

A program written using the JSP Session mechanism can be a shopping cart program with powerful functions. Is it very attractive? Let's get started with our program.

Product class constructed in one of the JSP Session mechanism shopping cart

◆ Write a Goods class, define the attributes of the product, return the methods of the product attributes, and compare the product objects.

◆ Goods. java

 
 
  1. package com.viita.Shop;  
  2.  
  3. public class Goods implements Comparable {  
  4.  

◆ Initialize member variables

 
 
  1. Private StringId=Null; // Product Id
  2. Private StringName=Null; // Product name
  3. Private floatPrice=0. 00F; // price of the product
  4. Private intNumber=0; // Number of items
  5. Public Goods (String Id, String name, float price, int number ){
  6. This. Id= Id;
  7. This. name= Name;
  8. This. price= Price;
  9. This. number= Number;
  10.  
  11. }
  12. Public String getId () // return the Id of the purchased item
  13. {
  14. Return this. Id;
  15. }
  16. Public String getName () // return the name of the purchased item
  17. {
  18. Return this. name;
  19. }
  20. Public float getPrice () // returns the price of the purchased item
  21. {
  22. Return this. price;
  23. }
  24. Public int getNumber () // returns the number of purchased items
  25. {
  26. Return this. number;
  27. }
  28. Public int compareTo (Object m ){
  29. // TODO Auto-generated method stub
  30.  
  31. GoodsN= (Goods) m;
  32. IntComRs=Id. CompareTo (n. Id );
  33. Return comRs;
  34.  
  35. }
  36.  
  37. }
  38.  

JSP Session mechanism shopping cart II shopping cart implementation

◆ Create Goods (product) object goods and create an ArrayList object ay

◆ Add () the item object to the ArrayList object ay using the ArrayList object Method

◆ Because the ArrayList object has a method to add and delete members, multiple product storage can be managed on the ArrayList object.

◆ Store the ArrayList object ay in the session object to implement the shopping cart function.

◆ Shopcar. jsp

 
 
  1. <%@ page language="java" import=" java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%> 
  2. <%  
  3.  

◆ Set the encoding format

 
 
  1. request.setCharacterEncoding("GBK");  
  2.  

◆ Obtain parameter information

 
 
  1. String id = request.getParameter("id");  
  2. String name = request.getParameter("name");  
  3. int number = java.lang.Integer.parseInt(request.getParameter("number"));  
  4. float price= java.lang.Float.parseFloat(request.getParameter("price")); 

◆ Create product objects and ArrayList objects

 
 
  1. Goods goods = new Goods(id,name,price,number);  
  2. ArrayList ay = null; 

◆ If the session has never been written, add the created item object to the ArrayList object and write it to the session.

 
 
  1. if((ArrayList)session.getAttribute("car")==null)  
  2. {  
  3. ay = new ArrayList();  
  4. ay.add(goods);  
  5. session.setAttribute("car",ay);  
  6. response.sendRedirect("order_index.jsp");  

◆ If it has been written, add the product object to the ArrayList object and write it to the session.

 
 
  1. else  
  2. {  
  3. ay=(ArrayList)session.getAttribute("car"); 

◆ If the ArrayList object is empty, it is directly added to the ArrayList object.

 
 
  1. if(ay.isEmpty())  
  2. {  
  3. ay.add(goods);  
  4. session.setAttribute("car",ay);  
  5. response.sendRedirect("order_index.jsp");  

◆ If the ArrayList object is not empty, determine whether the purchased product already exists in the vehicle.

 
 
  1. Else
  2. {
  3. IteratorIt=Ay. Iterator ();
  4. For (intI=0; I<Ay. size(); I ++) // There is another Traversal method below
  5. {
  6. GoodsShop= (Goods) it. next ();

◆ If the purchased product already exists, print the input prompt.

 
 
  1. If (shop. compareTo (goods) = 0)
  2. {
  3. Out. println ("<Script>Alert ('you have purchased this item! '); Window. close (); Script>");
  4. }

◆ If the purchased item does not exist, add the item to the ArrayList object and write it to the session.

 
 
  1. else  
  2. {  
  3. ay.add(goods);  
  4. session.setAttribute("car",ay);  
  5. response.sendRedirect("order_index.jsp");  
  6. }  
  7. }  
  8. }  
  9. }   
  10. %> 

JSP Session mechanism shopping cart 3 Delete items

◆ Delete the items in the shopping cart

◆ RemoveGoods. jsp

 
 
  1. <%@ page language="java" import="java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%> 
  2. <%  

◆ Set the encoding format

 
 
  1. request.setCharacterEncoding("gb2313"); 

◆ Obtain parameter information

 
 
  1. String id = request.getParameter("id");  
  2. String name = request.getParameter("name");  
  3. float price = java.lang.Float.parseFloat(request.getParameter("price"));  
  4. int number = java.lang.Integer.parseInt(request.getParameter("number")); 

◆ Create a commodity object that meets the condition parameters to be deleted

 
 
  1. Goods goods = new Goods(id,name,price,number); 

◆ Get the ArrayList object stored in the session

 
 
  1. ArrayList ay = (ArrayList)session.getAttribute("car");  
  2. Iterator it = ay.iterator(); 

◆ Traverse the ArrayList object and compare the elements in the ArrayList object with the created items that meet the parameter conditions to be deleted

 
 
  1. for(int i = ay.size();it.hasNext();i--)  
  2. {  
  3. Goods shop = (Goods)it.next(); 

◆ Check whether ArrayList objects have the same elements as the items to be deleted.

 
 
  1. if(shop.compareTo(goods)==0)  
  2. {  
  3. int index = ay.indexOf(shop);  

◆ If the ArrayList object is already empty, jump

 
 
  1. if(ay.isEmpty())  
  2. {  
  3. response.sendRedirect("order_index.jsp");  

◆ If the ArrayList object is not empty, the elements that match the condition of the item to be deleted are removed from it, and the session is rewritten.

 
 
  1. Else
  2. {
  3. Ay. remove (index );
  4. Session. setAttribute ("car", ay );
  5. Response. sendRedirect ("order_index.jsp ");
  6. }
  7. }
  8. Else
  9. {
  10. Out. print ("program exception ");
  11. }
  12. }
  13. %> 

Does the JSP Session mechanism's shopping cart make your eyes shine suddenly? Let's get started. We look forward to your attempt to use the JSP Session mechanism.

  1. Storage and display of images in databases Based on JSP
  2. JSP tutorial basics: Technical Features of JSP
  3. Five Aspects to get started with JSP
  4. JSP2.0 features of basic JSP tutorial knowledge
  5. JSP tutorial-traffic count JSP source code

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.