Jsp + Servlet to implement the shopping cart function, servlet shopping cart

Source: Internet
Author: User

Jsp + Servlet to implement the shopping cart function, servlet shopping cart

This article provides examples of how to use Servlet to implement the shopping cart function for your reference. The details are as follows:

(1) Use servlet to implement a simple Shopping Cart system. The Project structure is as follows: (to create a web Project, only AddItemServlet and ListItemServlet are required. Exam403.jsp. Others)

 

(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 "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "/> <title> untitled document </title> 

(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 shopping 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. setAtt Rigoal ("shoppingCar", shoppingCar);} else // assume that the input item ID number is null or the number is null. The System. out. print ("the item ID 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:

Question: http: // localhost: 8080/servletProject/exam403.jsp submit the product


Click to view the shopping cart:


OK!

This is the end of a simple shopping cart!

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.