Servlet/Jsp implement 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 = "" %>
Untitled document
(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 () +"
") ;}} 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!