JSP training (15) -- shopping cart instances (below) and summary

Source: Internet
Author: User

Main Content: l complete other basic functions of the shopping cart; l generate orders; l Summary 1. Other functions of the shopping cart modify the number of items in the shopping cart and delete items are two basic functions. The implementation process is similar to adding a project. Here, only the reference code is provided: 1.1 Model Part file name: cartmanager. java (modified based on the previous lecture, and the red part is the added content) package JavaBean; import Java. util. arraylist; import Java. util. iterator; public class cartmanager {// indicates the shopping carprivate arraylist cart; Public void setcart (arraylist cart) {This. cart = cart;} public arraylist getcart () {return cart;} // Add item & quantity public arraylist addtocart (goods g, int quantity) {If (Cart = NULL) {// instantiate the cart object ca RT = new arraylist (); // Add to cart item = new item (g, quantity); cart. add (item);} else {// convert to array object items [] = cart. toarray (); Boolean find = false; // indicates whether the for (INT I = 0; I <items. length; I ++) {item temp = (item) items [I]; // determines whether the item to be added exists in the shopping cart if (temp. getgoods (). getgoodsid (). equals (G. getgoodsid () {temp. setquantity (temp. getquantity () + quantity); find = true; break ;}} if (! Find) {// Add to the shopping cart item = new item (g, quantity); cart. add (item) ;}} return cart;} public void Delete (string goodsid) {// convert to iterator object iterator I = cart. iterator (); While (I. hasnext () {// get a shopping item temp = (item) I. next (); If (temp. getgoods (). getgoodsid (). equals (goodsid) {cart. remove (temp); break ;}} public void Update (string goodsid, int quantity) {iterator I = cart. iterator (); While (I. Hasnext () {// get a shopping item temp = (item) I. next (); If (temp. getgoods (). getgoodsid (). equals (goodsid) {temp. setquantity (Quantity); break ;}}} 1.2 modify part of the View File Name: cart. JSP (modified based on the previous lecture, and the red part is the added content) <% @ page contenttype = "text/html; charset = gb2312 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> information in the cart <br> <Table border = 1> <tr> <TD> item No. </TD> <TD> item name </TD> <TD> price </TD> <TD> quantity </TD> </tr> <C: fore Ach Var = "item" items = "$ {cart}"> <tr> <form action = "processcart"> <input type = "hidden" name = "goodsid" value = "$ {item. goods. goodsid} "> <TD >$ {item. goods. goodsid} </TD> <TD >$ {item. goods. goodsname} </TD> <TD >$ {item. goods. price }</TD> <input type = "text" name = "quantity" value = "$ {item. quantity} "> </TD> <input type =" Submit "name =" action "value =" modify "> </TD> <input type = "Submit" name = "action" value = "delete"> </TD> </form> </tr> </C: foreach> </table> 1.3 The same controller is used for the delete and modify functions of the controller, the function to be completed is determined based on the value of the submit button. The reference code is as follows: File Name: processcart. javapackage servlet; import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; import JavaBean. *; import Java. util. *; public class processcart extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {// response. s Etcontenttype ("text/html; charset = gb2312"); // printwriter out = response. getwriter (); try {// request. setcharacterencoding ("gb2312"); // you can specify string goodsid = request. getparameter ("goodsid"); // run the following command to delete or modify string action = request. getparameter ("action"); Action = new string (action. getbytes ("8859_1"); // out. println (action); string quantity = NULL; If (action. equals ("modify") {quantity = request. g Etparameter ("quantity");} // create the JavaBean object cartmanager = new cartmanager (); // obtain the session object httpsession session = request. getsession (true); // get the cart of the cart object arraylist cart = (arraylist) session. getattribute ("cart"); // use cart to initialize cartmanager. setcart (Cart); If (action. equals ("modify") {cartmanager. update (goodsid, integer. parseint (Quantity);} else {cartmanager. delete (goodsid);} // Save the shopping cart Session session. setattribute ("cart", cartmanager. getcart ();} catch (exception e) {// out. println (E. tostring ();} response. sendredirect ("cart. JSP ");} public void dopost (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {doget (request, response);} 2. generate an order if the user finishes his/her purchase, generally, you need to enter personal delivery information and store the order information in the database. This section describes how to organize information. The process of storing information is not described here. Two tables are required: l order table l order list order table information: l order number: date + sequential number, usually used as the primary key l order issuer: usually get information from the session or ask the user to enter (without logon) l order date: l order status: l payment method l delivery address l contact number l recipient order list (Order Number and item number are used as joint primary keys ): l order No. l number of an item l the number of this item is recommended for transaction processing when the order is added. 3. Summary The entire JSP training is basically conducted in the MVC mode. You can follow this idea to complete any function. 3.1 MVC thinking analysis MVC: Every function is divided into three parts. L take V into consideration first: It indicates an attempt to deliver the data to the user and input and output the data respectively. L m: model, which is usually written using JavaBean. For each function, one method (or multiple methods) is actually used ), we should consider the conditions (Method Input) required during the execution of the entire method, how to feedback (Return Value Type and exception type), and use Java code for specific functions. L c: Remember 4 Sentences: n get request information: request. getparameter method, getparametervalues method N call JavaBean: Create an object, instantiate, initialize, call method N pass value: request. setattributen redirection: requestdispatcher or response. the sendredirect method is not difficult to grasp because the functions of the controller are very fixed. The model part belongs to the Java code and has little to do with the JSP syntax. Therefore, for readers, the difficulty of learning JSP is mainly in the view part. 3.2 input and output input: l the most basic method is form submission. Use form elements to submit information and specify the processing file (Controller) through the action attribute of form ). Typical use of hidden domains (modify information and delete ). L hyperchain method: a typical usage is to use "?" Pass parameters, such as display and delete by PAGE (Hyperlink mode ). L use the address bar. These are also very fixed. Output: l static information output: Header, directly output l single object: for example, user, use $ {user. property name} output l collection object: <C: foreach> label output l error information, including the prompt information: $ {error} l information output under specific conditions: <C: if> label processing is relatively flexible, mainly because of the flexible use of jstl and El. Therefore, we need to master jstl and El. 3.3 Relationship Between Files static: Relationship Between Files (multiple requests): button submission, hyperlink, address bar input Dynamics: Relationship Between Files (one request): <JSP: forward> <JSP: Include> response. sendredirect requestdispatcher <% @ include file = "%>. 3.4 Information Storage: l persistent storage: database storage, mainly using JDBC technology (O/R ing, JPA ). L temporary storage: memory storage, request (passing values in MVC mode), Session (login user information, shopping cart information), and appliction (public chat room) training notes are all over. My contact: lixucheng@dl.cnhttp: // blog.csdn.net/javaeeteacherreference material: Drawing Java Web programming basics tutorial
Related Article

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.