Servlet/Jsp implement shopping cart and servletjsp shopping cart

Source: Internet
Author: User

Servlet/Jsp implement shopping cart and servletjsp shopping cart

(1) Use servlet to implement a simple Shopping Cart system. The Project structure is as follows: (to create a web Project, you only need to add three files: AddItemServlet, ListItemServlet, and exam403.jsp. Do not worry about other files)


(2) The exam403.jsp code is as follows:

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

(3) The AddItemServlet code is as follows:

Package com. lc. shoppingCar; import javax. servlet. *; import javax. servlet. http. *; import java. io. *; import java. util. *; public class AddItemServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException {ServletContext application = getServletContext (); ServletConfig config = getServletConfig (); response. setContentTyp E ("text/html; charset = gb2312"); PrintWriter out = response. getWriter (); HttpSession session = request. getSession (); request. setCharacterEncoding ("gb2312"); // read the product ID and quantity passed in the form. String id = request. getParameter ("itemID"); String num = request. getParameter ("quantity"); if (id! = Null & num. length ()! = 0) {// read the shopping cart HashMap shoppingCar = (HashMap) session from Sessionn. getAttribute ("shoppingCar"); if (shoppingCar = null) shoppingCar = new HashMap (); // Add the item to the cart String onum = (String) shoppingCar. get (id); if (onum = null) shoppingCar. put (id, num); else {int n1 = Integer. parseInt (num); int n2 = Integer. parseInt (onum); String result = String. valueOf (n1 + n2); shoppingCar. put (id, result);} // write the shopping cart back to the session to save the session. setAttr Ibute ("shoppingCar", shoppingCar);} else // If the input item ID number is blank or the number is blank, the System is displayed. out. print ("the ID of a product is blank, or the number is blank! "); // Return to the product list page response. sendRedirect ("/servletProject/exam403.jsp");} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException {doGet (request, response );}}

(4) The ListItemServlet code is as follows:

Package com. lc. shoppingCar; import javax. servlet. *; import javax. servlet. http. *; import java. io. *; import java. util. *; public class ListItemServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException {ServletContext application = getServletContext (); ServletConfig config = getServletConfig (); response. setContentTy Pe ("text/html; charset = gb2312"); PrintWriter out = response. getWriter (); HttpSession session = request. getSession (); request. setCharacterEncoding ("gb2312"); // obtain the cart HashMap shoppingCar = (HashMap) session from the session. getAttribute ("shoppingCar"); // if (shoppingCar! = Null) {Set show = shoppingCar. entrySet (); Iterator it = show. iterator (); while (it. hasNext () {out. print (it. next () + "<br>") ;}} else out. print ("the shopping cart is empty! ");} Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException {doGet (request, response );}}

(5) The implementation effect is as follows:

Visit: http: // localhost: 8080/servletProject/exam403.jsp to learn how to submit the product


Click to view the shopping cart:


OK! This is the end of a simple shopping cart!


JSP interacts with Javascript Servlet to add a shopping cart.

Use form requests or xmlhttprequest.

Question about jsp shopping cart implementation

Your problem is a bit too long. You mean that the session has been saved, and your page jumps.
The page can be implemented using ajax without navigation. We recommend that you do not place the page in the session, because the online shopping mall system will see the items in the shopping cart during your next login, which are all stored in the database.
Session itself is a hash of key and value.
 

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.