JavaScript中如何调用Java方法


Posted in Javascript onSeptember 16, 2020

在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]); // 删除相应节点
            //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 相关文章推荐
jQuery代码优化 事件委托篇
Nov 01 Javascript
统计jQuery中各字符串出现次数的工具
May 03 Javascript
JavaScript中__proto__与prototype的关系深入理解
Dec 04 Javascript
全面理解面向对象的 JavaScript(来自ibm)
Nov 10 Javascript
jquery ajax跨域解决方法(json方式)
Feb 04 Javascript
分享我对JS插件开发的一些感想和心得
Feb 04 Javascript
jQuery grep()方法详解及实例代码
Oct 30 Javascript
jQuery表单设置值的方法
Jun 30 jQuery
vue cli webpack中使用sass的方法
Feb 24 Javascript
微信小程序使用map组件实现检索(定位位置)周边的POI功能示例
Jan 23 Javascript
vue百度地图 + 定位的详解
May 13 Javascript
JS函数式编程实现XDM一
Jun 16 Javascript
Vue封装全局过滤器Filters的步骤
Sep 16 #Javascript
Vue父子组件传值的一些坑
Sep 16 #Javascript
vue-cli3项目打包后自动化部署到服务器的方法
Sep 16 #Javascript
vue项目打包后提交到git上为什么没有dist这个文件的解决方法
Sep 16 #Javascript
vue 自定指令生成uuid滚动监听达到tab表格吸顶效果的代码
Sep 16 #Javascript
vue中选中多个选项并且改变选中的样式的实例代码
Sep 16 #Javascript
vue实现div可拖动位置也可改变盒子大小的原理
Sep 16 #Javascript
You might like
用在PHP里的JS打印函数
2006/10/09 PHP
模仿OSO的论坛(五)
2006/10/09 PHP
一个分页的论坛
2006/10/09 PHP
Laravel5.1 框架Request请求操作常见用法实例分析
2020/01/04 PHP
利用腾讯的ip地址库做ip物理地址定位
2010/07/24 Javascript
node.js中的forEach()是同步还是异步呢
2015/01/29 Javascript
JS表的模拟方法
2015/02/05 Javascript
常用的JavaScript模板引擎介绍
2015/02/28 Javascript
chrome不支持form.submit的解决方案
2015/04/28 Javascript
js过滤HTML标签完整实例
2015/11/26 Javascript
详解JavaScript的Date对象(制作简易钟表)
2020/04/07 Javascript
jQuery对html元素的取值与赋值实例详解
2015/12/18 Javascript
快速掌握Node.js之Window下配置NodeJs环境
2016/03/21 NodeJs
动态的9*9乘法表效果的实现代码
2016/05/16 Javascript
JS全局变量和局部变量最新解析
2016/06/24 Javascript
JS控制页面跳转时未请求要跳转的地址怎么回事
2016/10/14 Javascript
用JavaScript做简易的购物车的代码示例
2017/10/20 Javascript
详解webpack 最简打包结果分析
2019/02/20 Javascript
Vue实现手机扫描二维码预览页面效果
2020/05/28 Javascript
[00:12]2018DOTA2亚洲邀请赛 sylar表现SOLO技艺
2018/04/06 DOTA
Python实现字典去除重复的方法示例
2017/07/31 Python
Python编程实现正则删除命令功能
2017/08/30 Python
TensorFlow深度学习之卷积神经网络CNN
2018/03/09 Python
浅谈python正则的常用方法 覆盖范围70%以上
2018/03/14 Python
Anaconda下安装mysql-python的包实例
2018/06/11 Python
基于scrapy的redis安装和配置方法
2018/06/13 Python
Python实现动态添加属性和方法操作示例
2018/07/25 Python
Python用于学习重要算法的模块pygorithm实例浅析
2018/08/16 Python
什么是python的列表推导式
2020/05/26 Python
如何Tkinter模块编写Python图形界面
2020/10/14 Python
美国益智玩具购物网站:Fat Brain Toys
2017/11/03 全球购物
怎么样写好简历中的自我评价
2013/10/25 职场文书
出纳员岗位职责风险
2014/03/06 职场文书
大学班级学风建设方案
2014/05/01 职场文书
自强自立美德少年事迹材料
2014/08/16 职场文书
银行工作心得体会范文
2016/01/23 职场文书