[英]Shopping Cart Using Servlet
I have a simple shopping cart code that add products from a drop-down list of games in an html page. 我有一个简单的购物车代码,可以在html页面的游戏下拉列表中添加产品。 I edited the html page, removed the drop down list and added an 'add to cart' button for each game. 我编辑了html页面,删除了下拉列表,并为每个游戏添加了“添加到购物车”按钮。 So I have been trying to edit the cart to add the games whenever any 'add to cart' button is clicked but couldn't. 因此,我一直在尝试编辑购物车以在单击“添加到购物车”中的任何按钮时添加游戏,但都没有。 Note that I am using jetty-9 servlets for server side code. 请注意,我正在将jetty-9 servlet用于服务器端代码。
Please help me !!! 请帮我 !!!
Here is the original cart code that worked with the drop down list: 这是可用于下拉列表的原始购物车代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Cart extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
String s, goods[] = {"Fifa 15", "Battlefield 5", "GTA 6"};
double price []={10,20,30};
double cost;
PrintWriter out = res.getWriter();
res.setContentType("text/html");
HttpSession session = req.getSession(true);
if ( session == null ) return;
for (int i = 0; i < goods.length; i++)
if ( session.getAttribute(goods[i]) == null )
session.setAttribute(goods[i], new Integer(0));
if ( (s = req.getParameter("buy")) != null ) {
int n = ((Integer)session.getAttribute(s)).intValue();
session.setAttribute(s, new Integer(n + 1));
}
out.println("<html><body><h2>Shopping Cart</h2><ul>");
for (int i = 0; i < goods.length; i++) {
int n = ((Integer)session.getAttribute(goods[i])).intValue();
if ( n > 0 ){
out.println("<li><b>" + goods[i] + "</b> : " + n +":"+ price[i] +"</li>");
cost=n*price[i];
out.println(cost);}
}
out.println("</ul></body></html>");
}
}
Hers is the html page of the games: Hers是游戏的html页面:
<html>
Games
<head>
</head>
<body>
<form action="http://localhost:8080/apps/Cart"method="post" >
Fifa 15 </br>
<img src="images/fifa-15.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" ></br>
Battlefield 4</br>
<img src="images/battlefield-4.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" name ="battle"></br>
GTA 5</br>
<img src="images/gta-5.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" name = "gta">
</form>
</body>
</html>
Finally your code should be this: 最后,您的代码应为:
HttpSession session = req.getSession(true);
for (int i = 0; i < goods.length; i++) {
if (session.getAttribute(goods[i]) == null)
session.setAttribute(goods[i], new Integer(0));
}
if ((s = req.getParameter("fifa")) != null) {
int n = ((Integer) session.getAttribute(goods[0])).intValue();
session.setAttribute(goods[0], new Integer(n + 1));
} else if ((s = req.getParameter("battle")) != null) {
int n = ((Integer) session.getAttribute(goods[1])).intValue();
session.setAttribute(goods[1], new Integer(n + 1));
} else if ((s = req.getParameter("gta")) != null) {
int n = ((Integer) session.getAttribute(goods[2])).intValue();
session.setAttribute(goods[2], new Integer(n + 1));
}
And html page: 和html页面:
Fifa 15 </br>
<img src="images/fifa-15.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" name = "fifa"></br>
Battlefield 4</br>
<img src="images/battlefield-4.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" name ="battle"></br>
GTA 5</br>
<img src="images/gta-5.jpg" width = 200 height = 300 alt="image" /></br>
<input type="submit" value="add to cart" name = "gta">
After detecting which button was pressed you can choose action for each button. 检测到按下了哪个按钮后,您可以为每个按钮选择操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.