亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript中如何調用Java方法

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

在JavaScript中想要調用Java的方法,我通過JavaScript訪問servlet,再通過servlet調用Java某個類的方法。

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">價格</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代碼

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

以上就是JavaScript中如何調用Java方法的詳細內容,更多關于js中調用Java方法的資料請關注腳本之家其它相關文章!

相關文章

  • JavaScript實現文本中間縮略的兩種方案

    JavaScript實現文本中間縮略的兩種方案

    項目中經常會遇到縮略展示文字的場景,即要求文字在一行不換行展示,超出自動展示...,常用的展示效果有兩種,文字中間縮略以及文字末尾縮略,本文將通過代碼示例給大家詳細的講一下這兩種方案,需要的朋友可以參考下
    2024-06-06
  • uniapp-路由uni-simple-router安裝配置教程

    uniapp-路由uni-simple-router安裝配置教程

    專為uniapp打造的路由器,和uniapp深度集成,uniapp用到了很多vue的api,但在路由管理的功能相對于vue-router還是比較欠缺的,比如全局導航守衛(wèi),本文給大家講解uniapp-路由uni-simple-router相關知識,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • 微信小程序swiper組件實現抖音翻頁切換視頻功能的實例代碼

    微信小程序swiper組件實現抖音翻頁切換視頻功能的實例代碼

    這篇文章主要介紹了微信小程序swiper組件實現抖音翻頁切換視頻功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • JS實現簡單的二維矩陣乘積運算

    JS實現簡單的二維矩陣乘積運算

    這篇文章主要介紹了JS實現簡單的二維矩陣乘積運算方法,涉及JavaScript基于數組操作實現矩陣運算的功能,需要的朋友可以參考下
    2016-01-01
  • 微信小程序實現同一頁面取值的方法分析

    微信小程序實現同一頁面取值的方法分析

    這篇文章主要介紹了微信小程序實現同一頁面取值的方法,結合實例形式分析了微信小程序在同一頁面取值的常見情況與具體操作技巧,需要的朋友可以參考下
    2019-04-04
  • 微信小程序與webview?H5交互的方法(內嵌H5跳轉原生頁面)

    微信小程序與webview?H5交互的方法(內嵌H5跳轉原生頁面)

    小程序webView中嵌套H5頁面,難免會遇到小程序與h5頁面進行數據通信或交互的場景,下面這篇文章主要給大家介紹了關于微信小程序與webview?H5交互的相關資料,內嵌H5跳轉原生頁面,需要的朋友可以參考下
    2022-11-11
  • DeviceOne 讓你一見鐘情的App快速開發(fā)平臺

    DeviceOne 讓你一見鐘情的App快速開發(fā)平臺

    DeviceOne是一個非常先進的App開發(fā)平臺,使用Javascript 構建原生體驗的移動應用程序,DeviceOne主要關注外觀和體驗,以及和你的應用程序的 UI 交互
    2016-02-02
  • JavaScript實現移動端簽字功能

    JavaScript實現移動端簽字功能

    這篇文章主要為大家詳細介紹了JavaScript實現移動端簽字功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • JS實現根據URL批量下載文件并壓縮成zip文件

    JS實現根據URL批量下載文件并壓縮成zip文件

    這篇文章主要為大家學習介紹了JS如何實現根據URL批量下載文件并壓縮成zip文件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-08-08
  • 利用uni-app開發(fā)App的超簡易教程

    利用uni-app開發(fā)App的超簡易教程

    uni-app是一個使用Vue.js開發(fā)所有前端應用的框架,開發(fā)者編寫一套代碼,可發(fā)布到iOS、Android、Web(響應式)、以及各種小程序,下面這篇文章主要給大家介紹了關于如何利用uni-app開發(fā)App的相關資料,需要的朋友可以參考下
    2022-11-11

最新評論