最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中如何調(diào)用Java方法

 更新時(shí)間:2020年09月16日 15:34:38   作者:默默不語  
這篇文章主要介紹了JavaScript中如何調(diào)用Java方法,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下

在JavaScript中想要調(diào)用Java的方法,我通過JavaScript訪問servlet,再通過servlet調(diào)用Java某個(gè)類的方法。

HTML代碼

<table id="cartTable">
  <thead>
    <tr>
      <th class="product_remove">
        <label>
          <input class="check-all check" type="checkbox"/>  全選
        </label>
        <a class="fl delete" id="deleteAll" href="javascript:;" rel="external nofollow" ><i class="fa fa-trash-o"></i></a>
      </th>
      <th class="product_thumb">圖片</th>
      <th class="product_name">名稱</th>
      <th class="product-price">價(jià)格</th>
       <th class="product_quantity">款式</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="product_remove">
        <input class="check-one check" type="checkbox"/>
      </td>
      <td class="product_thumb">
        <a href="ShopShoesDetails.jsp?shoes_id=<%=shoes.getShoes_id() %>" rel="external nofollow" rel="external nofollow" >
          <img src="${pageContext.request.contextPath}/ShopServlet?method=getShopsShoesImage&shoes_id=<%=shoes.getShoes_id() %>" alt="">
        </a>
      </td>
      <td class="product_name">
        <a href="ShopShoesDetails.jsp?shoes_id=<%=shoes.getShoes_id() %>" rel="external nofollow" rel="external nofollow" ><%=shoes.getBrand() %>/<%=shoes.getSeries() %>/<%=shoes.getName() %>
        </a>
      </td>
      <td class="product-price"><%=shoes.getPrice() %></td>
      <td class="product_quantity"><%=shoes.getSex() %>/<%=shoes.getSize() %></td>
    </tr>
  </tbody>
</table>

ShopShoesDao.java

public void deleteFromCart(String shoes_id) {
    System.out.println("ShopShoesDao.deleteFromCart");
    String[] shoes_ids = shoes_id.split(",");
    
    Connection connection = DBUtil.getConnection();
    PreparedStatement preparedStatement = null;
    
    try {
      for (String string : shoes_ids) {
        int id = Integer.parseInt(string);
        String sql = "delete from user_product_cart where shoes_id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.executeUpdate();
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      DBUtil.close(preparedStatement);
      DBUtil.close(connection);
    }
  }

ShopServlet.java

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("service");
    request.setCharacterEncoding("UTF-8");
    String method=request.getParameter("method");
    System.out.println(method);
    if(method.equals("addProduct")) {
      addProduct(request,response);
    }else if(method.equals("getShopsShoesImage")) {
      try {
        getShopsShoesImage(request,response);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }else if(method.equals("addToCart")) {
      try {
        addToCart(request, response);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }else if(method.equals("deleteFromCart")) {
      try {
        deleteFromCart(request, response);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }else if(method.equals("payFromCart")) {
      try {
        payFromCart(request, response);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
private void deleteFromCart(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
    // TODO Auto-generated method stub
    System.out.println("deleteFromCart");
    request.setCharacterEncoding("UTF-8");
    int user_id= Integer.parseInt(request.getParameter("user_id"));
    System.out.println(user_id);
    String shoes_id = request.getParameter("shoes_id");
    System.out.println(shoes_id);
    ShopShoesDao shopShoesDao = new ShopShoesDao();
    shopShoesDao.deleteFromCart(shoes_id);
    request.getSession().setAttribute("shoes_id", shoes_id);
    response.sendRedirect(request.getContextPath()+"/cart.jsp");
  }

javascrip代碼

// 點(diǎn)擊全部刪除
  deleteAll.onclick = function () {
    if (selectedTotal.innerHTML != 0) {
      var con = confirm('確定刪除所選商品嗎?'); //彈出確認(rèn)框
      if (con) {
        var shoes_id = '';
        for (var i = 0; i < tr.length; i++) {
          // 如果被選中,就刪除相應(yīng)的行
          if (tr[i].getElementsByTagName('input')[0].checked) {
            shoes_id = shoes_id + tr[i].cells[0].innerHTML + ',';
            
            //tr[i].parentNode.removeChild(tr[i]); // 刪除相應(yīng)節(jié)點(diǎn)
            //i--; //回退下標(biāo)位置
          }
        }
        alert(shoes_id);
        window.location.href="ShopServlet?method=deleteFromCart&shoes_id=" rel="external nofollow" +shoes_id+"&user_id="+22;
        alert("刪除成功!");
      }
    } else {
      alert('請(qǐng)選擇商品!');
    }
    getTotal(); //更新總數(shù)
  }

以上就是JavaScript中如何調(diào)用Java方法的詳細(xì)內(nèi)容,更多關(guān)于js中調(diào)用Java方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • javascript 局部頁面打印實(shí)現(xiàn)代碼

    javascript 局部頁面打印實(shí)現(xiàn)代碼

    Web打印有很多方式,水晶報(bào)表使用的恐怕比較多,但這東西是收費(fèi)軟件,老板說不能用:(。
    2009-08-08
  • JavaScript創(chuàng)建對(duì)象的七種方式(推薦)

    JavaScript創(chuàng)建對(duì)象的七種方式(推薦)

    JavaScript創(chuàng)建對(duì)象的方式有很多,通過Object構(gòu)造函數(shù)或?qū)ο笞置媪康姆绞揭部梢詣?chuàng)建單個(gè)對(duì)象,顯然這兩種方式會(huì)產(chǎn)生大量的重復(fù)代碼,并不適合量產(chǎn)。接下來介紹七種非常經(jīng)典的創(chuàng)建對(duì)象的方式,他們也各有優(yōu)缺點(diǎn)
    2017-06-06
  • 前端實(shí)現(xiàn)文本溢出展開和收起功能

    前端實(shí)現(xiàn)文本溢出展開和收起功能

    在現(xiàn)代網(wǎng)頁設(shè)計(jì)中,文本是網(wǎng)頁中最重要的內(nèi)容之一,然而,當(dāng)文本超出其容器的大小時(shí),會(huì)發(fā)生文本溢出的問題,文本溢出不僅會(huì)影響網(wǎng)頁的視覺效果,還會(huì)影響網(wǎng)頁的可讀性和可用性,所以本文給大家介紹了前端實(shí)現(xiàn)文本溢出展開和收起功能的方法,需要的朋友可以參考下
    2025-04-04
  • JavaScript獲取Excel表格的列序號(hào)和列名

    JavaScript獲取Excel表格的列序號(hào)和列名

    這篇文章主要介紹了JavaScript獲取Excel表格的列序號(hào)和列名,在處理Excel文件時(shí),通常要獲取xx列的數(shù)據(jù),這就要求先找到列序號(hào),下文關(guān)于列名獲取需要的小伙伴可以參考一下
    2022-05-05
  • JS如何判斷移動(dòng)端訪問設(shè)備并解析對(duì)應(yīng)CSS

    JS如何判斷移動(dòng)端訪問設(shè)備并解析對(duì)應(yīng)CSS

    本文為大家詳細(xì)介紹下JS如何判斷移動(dòng)端訪問設(shè)備并解析對(duì)應(yīng)CSS,感興趣的朋友可以參考下
    2013-11-11
  • 在JavaScript中生成不可修改屬性對(duì)象的方法

    在JavaScript中生成不可修改屬性對(duì)象的方法

    這篇文章主要介紹了在 JavaScript 中生成不可修改屬性對(duì)象的方法,包括相關(guān)函數(shù)及原理,并列舉了在狀態(tài)管理、數(shù)據(jù)緩存、函數(shù)式編程等場(chǎng)景中的實(shí)際應(yīng)用,還通過代碼示例進(jìn)行了詳細(xì)說明,需要的朋友可以參考下
    2024-12-12
  • JavaScript表單驗(yàn)證的兩種實(shí)現(xiàn)方法

    JavaScript表單驗(yàn)證的兩種實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了JavaScript表單驗(yàn)證的兩種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • layui點(diǎn)擊彈框頁面 表單請(qǐng)求的方法

    layui點(diǎn)擊彈框頁面 表單請(qǐng)求的方法

    今天小編就為大家分享一篇layui點(diǎn)擊彈框頁面 表單請(qǐng)求的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • 詳解CocosCreator中幾種計(jì)時(shí)器的使用方法

    詳解CocosCreator中幾種計(jì)時(shí)器的使用方法

    這篇文章主要介紹了CocosCreator中幾種計(jì)時(shí)器的使用方法,推薦使用schedule,功能多些,銷毀時(shí)還能自動(dòng)移除
    2021-04-04
  • 不錯(cuò)的用外部Javascript修正特定網(wǎng)頁內(nèi)容

    不錯(cuò)的用外部Javascript修正特定網(wǎng)頁內(nèi)容

    不錯(cuò)的用外部Javascript修正特定網(wǎng)頁內(nèi)容...
    2007-08-08

最新評(píng)論

齐齐哈尔市| 霍山县| 朝阳区| 沁源县| 巫溪县| 会同县| 河南省| 开江县| 连云港市| 兴义市| 元阳县| 长泰县| 肥乡县| 张家口市| 霍城县| 当涂县| 龙山县| 仲巴县| 维西| 嵩明县| 曲沃县| 泰安市| 静乐县| 抚松县| 蚌埠市| 广丰县| 平度市| 西城区| 扶风县| 右玉县| 永昌县| 渭源县| 馆陶县| 海原县| 富源县| 咸丰县| 弋阳县| 河曲县| 札达县| 霍山县| 泸西县|