南强小屋 Design By 杰米

在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" rel="external nofollow" rel="external nofollow" >
          <img src="/UploadFiles/2021-04-02/ShopServlet">

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]); // 删除相应节点
            //i--; //回退下标位置
          }
        }
        alert(shoes_id);
        window.location.href="ShopServlet" rel="external nofollow" +shoes_id+"&user_id="+22;
        alert("删除成功!");
      }
    } else {
      alert('请选择商品!');
    }
    getTotal(); //更新总数
  }

以上就是JavaScript中如何调用Java方法的详细内容,更多关于js中调用Java方法的资料请关注其它相关文章!

标签:
JavaScript,调用Java方法,JavaScript,调用方法

南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米

评论“JavaScript中如何调用Java方法”

暂无JavaScript中如何调用Java方法的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。