您好,登錄后才能下訂單哦!
小編給大家分享一下eclipse中web項(xiàng)目如何實(shí)現(xiàn)Javaweb購(gòu)物車,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體實(shí)現(xiàn):
首先我們要看一下項(xiàng)目整體的結(jié)構(gòu)
下面我們要先創(chuàng)建實(shí)體類,就是我們的商品、預(yù)購(gòu)商品和購(gòu)物車這三個(gè)實(shí)體類。
Beans
Cart類(這個(gè)類是購(gòu)物車實(shí)體類,包含了購(gòu)物車中添加的商品和總計(jì)兩個(gè)屬性。)
package Beans; import java.util.HashMap; public class Cart { private HashMap<String,CartItem> cartItems=new HashMap<String,CartItem>();//購(gòu)物車中添加的商品 private double total;//總計(jì) public HashMap<String, CartItem> getCartItems() { return cartItems; } public void setCartItems(HashMap<String, CartItem> cartItems) { this.cartItems = cartItems; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
CartItem類(這個(gè)是購(gòu)物車中添加的商品類,包含有商品、商品個(gè)數(shù)和小計(jì))
package Beans; public class CartItem { private Product product;//商品 private int buyNum;//個(gè)數(shù) private double subTotal;//小計(jì) public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getBuyNum() { return buyNum; } public void setBuyNum(int buyNum) { this.buyNum = buyNum; } public double getSubTotal() { return subTotal; } public void setSubTotal(double subTotal) { this.subTotal = subTotal; } }
Product類 (這里是具體的商品類,包含有商品編號(hào)、商品名和商品價(jià)格三個(gè)屬性)
package Beans; public class Product { private String pid;//商品編號(hào) private String name;//商品名 private double price;//商品價(jià)格 public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Product(String pid,String name,double price) { // TODO Auto-generated constructor stub this.pid = pid; this.name = name; this.price = price; } }
Service
這個(gè)包下面只有一個(gè)類,主要的作用是存入商品,并能根據(jù)商品編號(hào)找到商品。
ProductService類
package Service; import java.util.HashMap; import Beans.CartItem; import Beans.Product; public class ProductService { private HashMap<String,CartItem> cartItems=new HashMap<String,CartItem>(); public ProductService() //構(gòu)造函數(shù) { CartItem cartltem1=new CartItem(); CartItem cartltem2=new CartItem(); Product product1=new Product("001","Mobilephone",1000); Product product2=new Product("002","Watch",100); cartltem1.setProduct(product1); cartltem2.setProduct(product2); cartItems.put("001",cartltem1); cartItems.put("002", cartltem2); } public Product findProductbypid(String pid) { CartItem cartItem=cartItems.get(pid); Product product=cartItem.getProduct(); return product; } }
Servelet
ProductServlet類 (在這經(jīng)常會(huì)報(bào)錯(cuò) 1、httpservelet類無(wú)法繼承;因?yàn)閔ttpservelet類是在tomcat下的所以這里可能是tomcat沒(méi)有配置入項(xiàng)目或者h(yuǎn)ttpservelet類沒(méi)有導(dǎo)入,所以要重新導(dǎo)入tomcat。2、dopost和doget兩種基礎(chǔ)方法使用錯(cuò)誤,導(dǎo)致頁(yè)面?zhèn)鱽?lái)的數(shù)據(jù)無(wú)法進(jìn)行處理;解決:servelet類中的方法要與頁(yè)面選擇方法一致。3、亂碼,中文亂碼;解決:中文的編碼最好用utf-8【servlet改編碼是對(duì)req、resp設(shè)置】,并且頁(yè)面和后臺(tái)采用的編碼要一致。)
這里的路徑配置采用的是標(biāo)簽(方便)、也可采用.xml配置.
package Servlet; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Beans.Cart; import Beans.CartItem; import Beans.Product; import Service.ProductService; @WebServlet("/productServlet") public class ProductServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ProductService productService = new ProductService(); String pid=(String)req.getParameter("Product"); int buyNum=Integer.parseInt(req.getParameter("number")); HttpSession session=req.getSession(); Cart cart=(Cart)session.getAttribute("cart"); if(cart==null) { cart=new Cart(); } CartItem cartItem=new CartItem(); cartItem.setBuyNum(buyNum); Product product=productService.findProductbypid(pid); cartItem.setProduct(product); double subTotal=product.getPrice()*buyNum; cartItem.setSubTotal(subTotal); HashMap<String, CartItem> cartItems=cart.getCartItems(); double newSubTotal=0; if(cartItems.containsKey(pid)) { CartItem item=cartItems.get(pid); int oldBuyNum= item.getBuyNum(); oldBuyNum=oldBuyNum+buyNum; item.setBuyNum(oldBuyNum); double oldSubTotal= item.getSubTotal(); newSubTotal=buyNum*product.getPrice(); oldSubTotal=oldSubTotal+newSubTotal; item.setSubTotal(oldSubTotal); } else { cartItems.put(pid, cartItem); newSubTotal=buyNum*product.getPrice(); } double total=cart.getTotal()+newSubTotal; cart.setTotal(total); cart.setCartItems(cartItems); session.setAttribute("cart", cart); req.getRequestDispatcher("cart.jsp").forward(req, resp); } }
cart.jsp
這里一定要導(dǎo)入其他類 ,用<%@ page import=""%>標(biāo)簽。
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="Beans.Cart" %> <%@ page import="Beans.CartItem" %> <%@ page import="Beans.Product" %> <%@page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% Cart cart=(Cart)request.getSession().getAttribute("cart"); if(cart==null) { %> <p>It is nothing!</p> <% } else{ HashMap<String, CartItem> cartItems=cart.getCartItems(); double total=cart.getTotal(); %> Your product list:<br> <% Set<String> keys=cartItems.keySet(); Iterator<String> iter = keys.iterator(); while(iter.hasNext()){ String key= iter.next(); CartItem cartItem=cartItems.get(key); Product product=cartItem.getProduct(); out.print(product.getName()+" ") ; out.println("price:"+product.getPrice()+"$") ; out.println("number:"+cartItem.getBuyNum()); }; %> <br> <% out.print(" total:"+total+"$"); } %> </body> </html>
index.jsp
在action=“”屬性的配置是不能只寫(xiě)后臺(tái)配置的“/productServlet”路徑,一定要加上<%=request.getContextPath() %>,否則有可能找不著路徑。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> Please select the item you want to buy.<br> <form action="<%=request.getContextPath() %>/productServlet" method="post"> Mobile phone(1000$) <input name="Product" type="radio" value="001" checked="checked"><br> Watch(100$) <input name="Product" type="radio" value="002"><br> please input the number! number:<input name="number" type="number"><br> <input type="submit" value="ok!"> </form> </body> </html>
以上是“eclipse中web項(xiàng)目如何實(shí)現(xiàn)Javaweb購(gòu)物車”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。