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
- package com.viita.Shop;
-
- public class Goods implements Comparable {
-
◆ Initialize member variables
- Private StringId=Null; // Product Id
- Private StringName=Null; // Product name
- Private floatPrice=0. 00F; // price of the product
- Private intNumber=0; // Number of items
- Public Goods (String Id, String name, float price, int number ){
- This. Id= Id;
- This. name= Name;
- This. price= Price;
- This. number= Number;
-
- }
- Public String getId () // return the Id of the purchased item
- {
- Return this. Id;
- }
- Public String getName () // return the name of the purchased item
- {
- Return this. name;
- }
- Public float getPrice () // returns the price of the purchased item
- {
- Return this. price;
- }
- Public int getNumber () // returns the number of purchased items
- {
- Return this. number;
- }
- Public int compareTo (Object m ){
- // TODO Auto-generated method stub
-
- GoodsN= (Goods) m;
- IntComRs=Id. CompareTo (n. Id );
- Return comRs;
-
- }
-
- }
-
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
- <%@ page language="java" import=" java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%>
- <%
-
◆ Set the encoding format
- request.setCharacterEncoding("GBK");
-
◆ Obtain parameter information
- String id = request.getParameter("id");
- String name = request.getParameter("name");
- int number = java.lang.Integer.parseInt(request.getParameter("number"));
- float price= java.lang.Float.parseFloat(request.getParameter("price"));
◆ Create product objects and ArrayList objects
- Goods goods = new Goods(id,name,price,number);
- 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.
- if((ArrayList)session.getAttribute("car")==null)
- {
- ay = new ArrayList();
- ay.add(goods);
- session.setAttribute("car",ay);
- response.sendRedirect("order_index.jsp");
- }
◆ If it has been written, add the product object to the ArrayList object and write it to the session.
- else
- {
- ay=(ArrayList)session.getAttribute("car");
◆ If the ArrayList object is empty, it is directly added to the ArrayList object.
- if(ay.isEmpty())
- {
- ay.add(goods);
- session.setAttribute("car",ay);
- response.sendRedirect("order_index.jsp");
- }
◆ If the ArrayList object is not empty, determine whether the purchased product already exists in the vehicle.
- Else
- {
- IteratorIt=Ay. Iterator ();
- For (intI=0; I<Ay. size(); I ++) // There is another Traversal method below
- {
- GoodsShop= (Goods) it. next ();
◆ If the purchased product already exists, print the input prompt.
- If (shop. compareTo (goods) = 0)
- {
- Out. println ("<Script>Alert ('you have purchased this item! '); Window. close (); Script>");
- }
◆ If the purchased item does not exist, add the item to the ArrayList object and write it to the session.
- else
- {
- ay.add(goods);
- session.setAttribute("car",ay);
- response.sendRedirect("order_index.jsp");
- }
- }
- }
- }
- %>
JSP Session mechanism shopping cart 3 Delete items
◆ Delete the items in the shopping cart
◆ RemoveGoods. jsp
- <%@ page language="java" import="java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%>
- <%
◆ Set the encoding format
- request.setCharacterEncoding("gb2313");
◆ Obtain parameter information
- String id = request.getParameter("id");
- String name = request.getParameter("name");
- float price = java.lang.Float.parseFloat(request.getParameter("price"));
- int number = java.lang.Integer.parseInt(request.getParameter("number"));
◆ Create a commodity object that meets the condition parameters to be deleted
- Goods goods = new Goods(id,name,price,number);
◆ Get the ArrayList object stored in the session
- ArrayList ay = (ArrayList)session.getAttribute("car");
- 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
- for(int i = ay.size();it.hasNext();i--)
- {
- Goods shop = (Goods)it.next();
◆ Check whether ArrayList objects have the same elements as the items to be deleted.
- if(shop.compareTo(goods)==0)
- {
- int index = ay.indexOf(shop);
◆ If the ArrayList object is already empty, jump
- if(ay.isEmpty())
- {
- 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.
- Else
- {
- Ay. remove (index );
- Session. setAttribute ("car", ay );
- Response. sendRedirect ("order_index.jsp ");
- }
- }
- Else
- {
- Out. print ("program exception ");
- }
- }
- %>
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.
- Storage and display of images in databases Based on JSP
- JSP tutorial basics: Technical Features of JSP
- Five Aspects to get started with JSP
- JSP2.0 features of basic JSP tutorial knowledge
- JSP tutorial-traffic count JSP source code