Previous user verification successful jump to hall.jsp, display products
hall.jsp
<%@ page language= "java" import= "java.util.*,com.wxh.domain.*" contenttype= "text/html"; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The next page hall.jsp ready for the displayed data in GohalluiCalled the Bookservice class
First of all
Book.java
Package Com.wxh.domain;public class Book {private int id;private string Name;private string publishhouse;private double PR ice;private int nums; The book's inventory private int shoppingnum=1;//buys the number of private String author;public int getshoppingnum () {return shoppingnum;} public void Setshoppingnum (int shoppingnum) {this.shoppingnum = Shoppingnum;} Public String Getauthor () {return author;} public void Setauthor (String author) {this.author = author;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getpublishhouse () {return publishhouse;} public void Setpublishhouse (String publishhouse) {this.publishhouse = Publishhouse;} Public double GetPrice () {return price;} public void Setprice (double price) {this.price = Price;} public int getnums () {return nums;} public void setnums (int nums) {this.nums = Nums;}}
Bookservice.java
Package Com.wxh.service;import Java.util.arraylist;import Com.wxh.domain.book;import com.wxh.utils.sqlhelper;// This is a business logic class for handling business related to book tables, and the public class Bookservice {//According to the number of books, returns a Bookpublic Getbookbyid (String ID) =new book (); String sql= "SELECT * from book where id=?"; String Paras[]={id}; ArrayList al=new SqlHelper (). executeQuery (SQL, paras), if (Al.size () ==1) {object[] obj= (object[]) al.get (0); Book.setid (Integer.parseint (Obj[0].tostring ())); Book.setname (obj[1].tostring ()); Book.setauthor (obj[2].tostring ()); Book.setpublishhouse (Obj[3].tostring ()); Book.setprice (Float.parsefloat (obj[4].tostring ())); Book.setNums ( Integer.parseint (Obj[5].tostring ()));} return book;} Get all the books information (paging) public ArrayList Getallbook () {String sql= "select * from book where 1 =?"; String paras[]={"1"}; ArrayList al=new SqlHelper (). executeQuery (SQL, paras); Arraylist<book> newal=new arraylist<book> ();//two Business encapsulation for (int i=0;i<al.size (); i++) {Object obj[]= ( Object[]) al.get (i); Book Book=new book (); Book.setid (INteger.parseint (Obj[0].tostring ())); Book.setname (obj[1].tostring ()); Book.setauthor (obj[2].tostring ()); Book.setpublishhouse (Obj[3].tostring ()); Book.setprice (Float.parsefloat (obj[4].tostring ())); Book.setNums ( Integer.parseint (Obj[5].tostring ())); Newal.add (book);} return Newal;}}
Implementation of the purchase functionA shopping cart has been created in Gohallui
Mycart.java
Package Com.wxh.service;import java.util.*; Import com.wxh.domain.book;//This represents my shopping cart public class Mycart {hashmap<string,book> hm=new hashmap<string,book >////Add book the second method public void AddBook2 (String id) {if (Hm.containskey (ID)) {//HM already has this book book=hm.get (ID); int Shoppingnum=book.getshoppingnum (); Book.setshoppingnum (shoppingnum+1);} Else{hm.put (ID, new Bookservice (). Getbookbyid (ID));}} Return the total price of the cart public float gettotalprice () {float totalprice=0.0f;//get total price arraylist<book> al=new arraylist< Book> (); Iterator It=hm.keyset (). Iterator (); while (It.hasnext ()) {//Remove ISBN String bookid= (string) it.next ();// Remove the BookBook book=hm.get (bookId) corresponding to the ISBN, Totalprice+=book.getprice () *book.getshoppingnum (); return totalprice;} Add Books public void Addbook (String id,book book) {if (Hm.containskey (ID)) {book=hm.get (ID));//If it has already been purchased, Shoppingnum quantity + 1int shoppingnum=book.getshoppingnum (); Book.setshoppingnum (shoppingnum+1);//hm.put (ID, book);} else{hm.put (ID, book);}} Delete book public void Delbook (String id) {hm.remove (id);}Update the book (for Cart, update the number of books purchased) public void Updatebook (String id,string nums) {//remove ID corresponding to BookBook book=hm.get (ID); Book.setshoppingnum (Integer.parseint (Nums));} Display all merchandise information in this cart public ArrayList showmycart () {arraylist<book> al=new arraylist<book> ();// Traverse Hashmapiterator It=hm.keyset (). iterator (); while (It.hasnext ()) {//Remove keystring id= (String) it.next ();//Remove BookBook Book=hm.get (ID); al.add (book);} Return al;} Empty the book, empty the shopping cart public void Clearbook () {hm.clear ();}}
Submit to Shoppingclservlet when you click on the purchase linkShoppingclservlet.java
The controller handles requests from users to purchase goods public class Shoppingclservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {request.setcharacterencoding ("utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out = Response.getwriter ();//Receive type value, differentiate what the user wants to do, del,update,addstring type=request.getparameter ("type"); if (Type.equals ("del")) {//Description the user wants to delete the product//receive the idstring Id=request.getparameter ("id") of the product the user wants to purchase;//Get the shopping cart (get the cart from the session) Mycart mycart= (Mycart) request.getsession () getattribute ("Mycart"); Mycart.delbook (ID);// Put the product information to be displayed in Requestrequest.setattribute ("Booklist", Mycart.showmycart ());// Jump back to ShowMyCart.jsprequest.getRequestDispatcher ("/web-inf/showmycart.jsp"). Forward (request, response);} else if (type.equals ("add")) {//Receive idstring Id=request.getparameter ("id") of the product that the user wants to purchase; SYSTEM.OUT.PRINTLN (ID);//When the shopping Cart object is created (when the user logs in successfully, create a shopping cart for him)//Remove the cart and add the book to the cart Mycart mycart= (mycart) Request.getsession (). getattribute ("Mycart");//equivalent to adding items to the shopping cart Mycart.addbooK2 (ID);/*//put the data to be displayed in the request, ready to be displayed. Request.setattribute ("Booklist", Mycart.showmycart ()); Request.setattribute ("Totalprice", MyCart.getTotalPrice () + "");//jump to show my cart to Request.getrequestdispatcher ("/web-inf/showmycart.jsp"). Forward (request, response); */// To prevent a page from refreshing, we can use Sendredirect () response.sendredirect ("/shopping/goshowmycart");} else if (type.equals ("Update")) {//update//Judge there is no book//Get the number of ISBN and quantity that the user wants to update ("id") bookids[]=request.getparametervalues;// Get the number of each book in string nums[]=request.getparametervalues ("Booknum"); Mycart mycart= (Mycart) request.getsession (). getattribute ("Mycart"); for (int i=0;i<bookids.length;i++) {// Update cart Mycart.updatebook (Bookids[i], nums[i]);} Jump back to my cart//Put the data you want to display in the request, ready to be displayed. Request.setattribute ("Booklist", Mycart.showmycart ()); Request.setattribute ("Totalprice", MyCart.getTotalPrice () + "");//jump to show my cart to Request.getrequestdispatcher ("/web-inf/showmycart.jsp"). Forward (request, response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexception {this.doget (request, Response);}}
———— from "Hanshunping detail JSP"
MVC Simple Shopping Cart project--Shopping Hall