1. Design of Shopping cart class
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc2hpewvxawfuz2xkaa==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
Shoppingcartitem: The book's package contains the title. Quantity, price three attributes, and corresponding getter and setter methods.
ShoppingCart: Shopping cart Package class. Items for map<string, shoppingcartitem>, and add shopping cart, get the total number of books in the shopping cart and the total price of three functions.
2:jsp Add Shopping cart. Bring the title and price into the hyperlink
<body><!--The purpose of increasing span is to locate--><div id= "Cartstatus" > You have <span id= "BookName" ></span> Added to the shopping cart, the shopping cart has <span id= "Totalbooknumber" ></span> book, the total Price is <span id= "Totalmoney" ></span></ div><br><br>java<ahref= "${pagecontext.request.contextpath}/addtocart?id=java&price=100" > Add Shopping Cart </a><br>ajax<ahref= "${pagecontext.request.contextpath}/addtocart?id=ajax&price=200 "> Add Shopping Cart </a><br>jquery<ahref=" ${pagecontext.request.contextpath}/addtocart?id=jquery& price=300 "> Add Shopping Cart </a><br></body>
<!--${pagecontext.request.contextpath} Gets the absolute path of the item--
Design of 3:addtocart-----servlet
Procedures such as the following:
1): Gets the request ID (bookname), price, which is obtained from the hyperlink in the JSP page
2): Gets the shopping Cart object in session, assuming there is no shopping cart in the session properties. A new shopping Cart object is placed in the session properties
3): Increase the shopping cart operation Shopping.addtocart (BookName, Price);
4): Want Ajax to pass the JSON object. The object contains: {"" BookName "": "Totalbooknumber", "Totalmoney"}, if the JSON object is returned from the server side. Then the attribute name must use double-cited!!
5): Response JSON request, Response.getwriter (). print (JSON);
public class Addtocartservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {this.dopost (request, response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { 1: Get the request parameter ID (bookname), pricestring bookname =request.getparameter ("id"); int price =integer.parseint ( Request.getparameter ("price"));//2: Gets the shopping cart object, ShoppingCart sc= (ShoppingCart) request.getsession () in session. GetAttribute ("SC"), if (sc==null) {sc=new shoppingcart (); Request.getsession (). SetAttribute ("SC", SC);} 3. Add the clicked object to the Cart Sc.addtocart (bookname, price);//4: Prepare the response JSON object: {"" BookName "": "Totalbooknumber", "Totalmoney"}// Returns a JSON object from the server side. Then the attribute name must use double-cited!!StringBuilder sbuilder=new StringBuilder (); Sbuilder.append ("{"). Append ("\" bookname\ ": \" "+bookname+" \ "). Append (", "). Append (" \ "Totalbook Number\ ": \" "+sc.gettotalbooknumber () +" \ ""). Append (","). Append ("\" totalmoney\ ": \" "+sc.gettotalmoney () +" \ ""). Append ("}"); Response JSON request Response.setcontenttype ("Text/javascript"); Response.getwriter (). Print (sbuilder.tostring ());}}
The use of StringBuilder to stitch JSON strings above can be simplified by third-party open-source Jackson:
String jsonstr=null;objectmapper objectmapper=new objectmapper (); jsonstr=objectmapper.writevalueasstring (SC);
4:ajax accept a reference from the server {"" BookName "": "Totalbooknumber", "Totalmoney"}
Steps:
1): Add a click response function to add a shopping cart to this hyperlink. And cancels the default behavior (return false)
2): load JSON data via HTTP GET request. $. getjson (URL, [data], [callback])
Prepare the Url.agrs and display the contents of the shopping cart in the JSP page inside the callback function.
3): Through the Hide (), show () method in jquery, infer that the shopping cart is not the first to use. Assume that it is used for the first time. The JSP page does not display the shopping cart.
Ajax Change Shopping Cart