我正在用 java 构建一个简单的电子商务解决方案,但我陷入了一个关键点。我无法显示正确的项目。我正在使用一个 servlet,它有一个 doGet 一个购物车类、一个产品类和 viewcart.jsp 文件。为了在 servlet 之间传递项目,我使用了上下文的 setAttribute。这是我到目前为止构建的文件。谁能帮我解决这个问题?每次我按添加到汽车时,它都会添加元素,但看起来之前的所有元素都会被我添加的最后一个元素覆盖。
package com.kd.ecommerce;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class addtocart
*/
@WebServlet("/addtocart")
public class addtocart extends HttpServlet {
private static final long serialVersionUID = 1L;
private shoppingcart shop = new shoppingcart();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext thisContext = getServletContext();
String name = request.getParameter("product");
Connection cn = DBConnect.getInstance();
try {
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("select * from inventory where name = '"+name+"' LIMIT 1");
while(rs.next()){
Products p = new Products(rs.getString(2),rs.getString(3),rs.getString(4),rs.getFloat(5));
System.out.println(p.getName()+":"+ p.getPrice());
shop.ins(p);
thisContext.setAttribute("shop", shop.getIt());
for(int i = 0; i< shop.getIt().size(); i++){
System.out.println(shop.getIt().get(i)+":"+ shop.getIt().get(i).getPrice());
}
response.sendRedirect("inventory.jsp?addedto=success");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Products.java
package com.kd.ecommerce;
public class Products {
private static String name;
private static Float price;
private static String desc;
private static String img;
public Products(String n, String d, String i,Float p){
name = n;
price = p;
desc = d;
img = i;
}
public String getName(){
return name;
}
public Float getPrice(){
return price;
}
public String getImage(){
return img;
}
public String getDesc(){
return desc;
}
}
购物车.java
package com.kd.ecommerce;
import java.util.ArrayList;
public class shoppingcart {
private static ArrayList<Products> items;
public shoppingcart(){
items = new ArrayList<Products>();
}
public void ins(Products p){
items.add(p);
}
public void remove(Products p){
items.remove(p);
}
public int getSize(){
return items.size();
}
public ArrayList<Products> getIt(){
return items;
}
}
viewcart.jsp
<jsp:include page="menu.jsp"></jsp:include>
<%
ServletContext sc = getServletConfig().getServletContext();
if(sc.getAttribute("shop") != null){
ArrayList<Products> it = (ArrayList<Products>)sc.getAttribute("shop");
out.println(it);
for(int i = 0; i< it.size(); i++){
out.println("<span class='price'>"+it.get(i).getPrice()+"</span>");
}
}else{
out.println("<span class='empty'>Shopping cart empty</span>");
}
%>
- 编辑 -
我使用会话的新 viewcart.jsp
<%
if(session.getAttribute("shop") != null){
shoppingcart sh = (shoppingcart)session.getAttribute("shop");
ArrayList<Products> pd = sh.getIt();
for(int i = 0; i< pd.size(); i++){
out.println("<span class='price'>"+pd.get(i).getPrice()+"</span>");
}
}else{
out.println("<span class='empty'>Shopping cart empty</span>");
}
%>