package com.test.servlet; import java.io.IOException; 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 com.test.bean.Cart; import com.test.dao.CartDao; /** * Servlet implementation class DelCartServlet */ @WebServlet("/Servlet/DelCartServlet") public class DelCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DelCartServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setHeader("Content-type","text/html;charset=utf-8") ; String gid=request.getParameter("gid"); if(gid!=null) { HttpSession session=request.getSession(); Cart cart=(Cart)session.getAttribute("cart"); if(cart!=null) { CartDao cartdao=new CartDao(); cartdao.delGoods(cart, Integer.parseInt(gid)); session.setAttribute("cart", cart); response.getWriter().println("删除成功"); }else { response.getWriter().println("删除失败"); } }else { response.getWriter().println("参数不正确"); } response.getWriter().println("<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车<a>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } 修改删除功能,在购物车物品数量大于1的情况下点击“删除”时减少1个,等于1时删除此项商品;
时间: 2024-03-05 15:48:03 浏览: 52
使用Java Servlet实现一个简单的在线留言板.txt
您的要求是修改 DelCartServlet 中的删除功能,使得在购物车物品数量大于1的情况下,点击“删除”按钮时减少1个商品,等于1时删除此项商品。
为了实现这个功能,您可以在 CartDao 类中添加一个新的方法,来实现删除商品的功能。这个方法可以接收两个参数:Cart 对象和商品 ID,然后根据商品 ID 找到对应的商品,将其数量减一或删除该商品。在 DelCartServlet 中,您可以调用这个新方法实现删除商品的功能。
以下是修改后的 DelCartServlet 代码:
```
package com.test.servlet;
import java.io.IOException;
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 com.test.bean.Cart;
import com.test.dao.CartDao;
/**
* Servlet implementation class DelCartServlet
*/
@WebServlet("/Servlet/DelCartServlet")
public class DelCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DelCartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type","text/html;charset=utf-8") ;
String gid=request.getParameter("gid");
if(gid!=null) {
HttpSession session=request.getSession();
Cart cart=(Cart)session.getAttribute("cart");
if(cart!=null) {
CartDao cartdao=new CartDao();
int count = cartdao.delGoods(cart, Integer.parseInt(gid));
if (count > 0) {
session.setAttribute("cart", cart);
response.getWriter().println("删除成功");
} else {
response.getWriter().println("删除失败");
}
}else {
response.getWriter().println("删除失败");
}
}else {
response.getWriter().println("参数不正确");
}
response.getWriter().println("<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
```
修改后的 CartDao 类中的 delGoods 方法如下:
```
public int delGoods(Cart cart, int gid) {
int count = 0;
for (int i = 0; i < cart.getItems().size(); i++) {
if (cart.getItems().get(i).getGoods().getGid() == gid) {
if (cart.getItems().get(i).getCount() > 1) {
cart.getItems().get(i).setCount(cart.getItems().get(i).getCount() - 1);
} else {
cart.getItems().remove(i);
}
count++;
break;
}
}
return count;
}
```
这个方法会返回删除的商品数量,如果返回值大于 0,说明删除成功,否则说明删除失败。
阅读全文