第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

刪除沒有反應

package servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import entity.itemsDAO;

import entity.Cart;

import entity.items;


public class CartServlet extends HttpServlet {


private String action ; //表示購物車的動作 ,add,show,delete

//商品業(yè)務邏輯類的對象

private itemsDAO idao = new itemsDAO();



/**

* Constructor of the object.

*/

public CartServlet() {

super();

}


/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}


/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*?

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

? ? ? ? doPost(request,response);

}


/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*?

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html;charset=utf-8");

PrintWriter out = response.getWriter();

if(request.getParameter("action")!=null)

{

this.action = request.getParameter("action");

if(action.equals("add")) //如果是添加商品進購物車

{

if(addToCart(request,response))

{

request.getRequestDispatcher("/success.jsp").forward(request, response);

}

else

{

request.getRequestDispatcher("/failure.jsp").forward(request, response);

}

}

if(action.equals("show"))//如果是顯示購物車

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

if(action.equals("delete")) //如果是執(zhí)行刪除購物車中的商品

{

if(deleteFromCart(request,response))

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

else

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

}

}


}


//添加商品進購物車的方法

private boolean addToCart( HttpServletRequest request, HttpServletResponse response)

{

String id = request.getParameter("id");

String number = request.getParameter("num");

items item = idao.getItemsById(Integer.parseInt(id));


//是否是第一次給購物車添加商品,需要給session中創(chuàng)建一個新的購物車對象

if(request.getSession().getAttribute("cart")==null)

{

Cart cart = new Cart();

request.getSession().setAttribute("cart",cart);

}

Cart cart = (Cart)request.getSession().getAttribute("cart");

if(cart.CartAdd(item, Integer.parseInt(number)))

{

return true;

}

else

{

return false;

}


}


//從購物車中刪除商品

private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response){

String id = request.getParameter("id");

Cart cart = (Cart) request.getSession().getAttribute("cart");

items it = idao.getItemsById(Integer.parseInt(id));

System.out.println(it.getName());

if(cart.CartRemove(it))

? ?{

? ? return true;

? ?}

? ?else

? ?{

? ? return false;

? ?}



}


/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}


}

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%@ page import="entity.Cart" %>

<%@ page import="entity.items" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <head>

? ? <base href="<%=basePath%>">

? ? <title>My JSP 'cart.jsp' starting page</title>

? <meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0"> ? ?

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

? ? <link type="text/css" rel="stylesheet" href="css/style1.css" />

? ? <script language="javascript">

? ?function delcfm() {

? ? ? ?if (!confirm("確認要刪除?")) {

? ? ? ? ? ?window.event.returnValue = false;

? ? ? ?}

? ?}

? ?</script>

? </head>

??

? <body>

? ?<h1>我的購物車</h1>

? ?<a href="index.jsp">首頁</a> >> <a href="index.jsp">商品列表</a>

? ?<hr>?

? ?<div id="shopping">

? ?<form action="" method="">

<table>

<tr>

<th>商品名稱</th>

<th>商品單價</th>

<th>商品價格</th>

<th>購買數(shù)量</th>

<th>操作</th>

</tr>

<%?

? //首先判斷session中是否有購物車對象

? if(request.getSession().getAttribute("cart")!=null)

? {

%>

<!-- 循環(huán)的開始 -->

? ? <%?

? ? ? ? Cart cart = (Cart)request.getSession().getAttribute("cart");

? ? ? ? HashMap<items,Integer> goods = cart.getGoods();

? ? ? ? Set<items> items = goods.keySet();

? ? ? ? Iterator<items> it = items.iterator();

? ? ? ??

? ? ? ? while(it.hasNext())

? ? ? ? {

? ? ? ? ? ?items i = it.next();

? ? %>?

<tr name="products" id="product_id_1">

<td class="thumb"><img src="images/<%=i.getPicture()%>" /><a href=""><%=i.getName()%></a></td>

<td class="number"><%=i.getPrice() %></td>

<td class="price" id="price_id_1">

<span><%=i.getPrice()*goods.get(i) %></span>

<input type="hidden" value="" />

</td>

<td class="number">

? ? ? ? ? ? ? ? ? ? ? <%=goods.get(i)%>

</td> ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? <td class="delete">

?<a href="servlet/CartServlet?action=delete&id=<%=i.getId()%>" ?onclick="delcfm();">刪除</a> ? ? ? ? ? ? ? ? ?

</td>

</tr>

? ? <%?

? ? ? ? }

? ? %>

<!--循環(huán)的結束-->


</table>

<div class="total"><span id="total">總計:<%=cart.CartTotal() %>¥</span></div>

?<%?

? ?}

%>

<div class="button"><input type="submit" value="" /></div>

</form>

</div>

? </body>

</html>

package entity;


import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;


//購物車類

public class Cart {

private HashMap<items, Integer> goods; //商品類和數(shù)量集合

private double total;//商品總價

//構造方法

public Cart(){

goods = new HashMap<items, Integer>();

total = 0.0;

}


public HashMap<items, Integer> getGoods() {

return goods;

}


public void setGoods(HashMap<items, Integer> goods) {

this.goods = goods;

}


public double getTotal() {

return total;

}


public void setTotal(double total) {

this.total = total;

}


//添加商品和商品數(shù)量進購物車 的方法

public boolean CartAdd(items i,int n){

if(goods.containsKey(i)){

goods.put(i, goods.get(i)+n);

}else{

goods.put(i, n);

}

CartTotal();//每次添加后重新計算總價格

return ?true;

}

//刪除商品的方法

public boolean CartRemove(items i){

goods.remove(i);

CartTotal();//每次刪除后重新計算總價格

return true;

}

//統(tǒng)計購物車內物品的總價格

public double CartTotal(){

double d = 0.0;

Set<items> keys = goods.keySet();//獲取鍵值對集合

Iterator<items> it = keys.iterator();//獲得迭代器對象

while (it.hasNext()){

items i = it.next();

d += i.getPrice()* goods.get(i);

}

this.setTotal(d);

return this.getTotal(); //返回總價格


}

// public static void main(String[] args) {

// //先創(chuàng)建兩個商品對象

// items t1 = new items(1,"沃特籃球鞋","溫州",200,500,"001.jpg");

// items t2 = new items(2,"李寧運動鞋","廣州",300,500,"002.jpg");

// items t3 = new items(1,"沃特籃球鞋","溫州",200,500,"001.jpg");

//

// //實例化購物車類

// Cart c = new Cart();

// c.CartAdd(t1, 3);

// c.CartAdd(t2, 1);

// c.CartAdd(t3, 1);

// c.CartRemove(t1);

// //遍歷購物車商品類集合

// Set<Map.Entry<items, Integer>> items = ?c.getGoods().entrySet();

// for(Map.Entry<items, Integer> obj: items){

// System.out.println(obj);

// }

//

// System.out.println("商品總價為:"+c.getTotal());

//

// }

}



正在回答

1 回答

檢查以下點刪除按鈕是的提交路徑,在Cart.jsp代碼里面

<td class="delete">

?<a href="servlet/CartServlet?action=delete&id=<%=i.getId()%>" onclick="delcfm();">刪除</a> ? ? ? ? ? ? ? ?

</td>


1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
JAVA遇見HTML——Servlet篇
  • 參與學習       160615    人
  • 解答問題       1086    個

本門課程在JSP課程的基礎上,深入介紹Servlet的基礎知識

進入課程

刪除沒有反應

我要回答 關注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號