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 相关文章推荐
JavaScript 事件查询综合
Jul 13 Javascript
js下用层来实现select的title提示属性
Feb 23 Javascript
JavaScript获取客户端计算机硬件及系统等信息的方法
Jan 02 Javascript
简介AngularJS的视图功能应用
Jun 17 Javascript
基于JavaScript将表单序列化类型的数据转化成对象的处理(允许对象中包含对象)
Dec 28 Javascript
Node.js DES加密的简单实现
Jul 07 Javascript
AngularGauge 属性解析详解
Sep 06 Javascript
Bootstrap提示框效果的实例代码
Jul 12 Javascript
jquery写出PC端轮播图实例
Jan 26 jQuery
微信小程序保存多张图片的实现方法
Mar 05 Javascript
js 数组 fill() 填充方法
Nov 02 Javascript
Vue.js中v-for指令的用法介绍
Mar 13 Vue.js
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获取浏览器信息类和客户端地理位置的2个方法
2014/04/24 PHP
php生成过去100年下拉列表的方法
2015/07/20 PHP
实例分析PHP将字符串转换成数字的方法
2019/01/27 PHP
tp5.1 框架路由操作-URL生成实例分析
2020/05/26 PHP
JavaScript 定义function的三种方式小结
2009/10/16 Javascript
基于jquery的代码显示区域自动拉长效果
2011/12/07 Javascript
使用JavaScript 实现对象 匀速/变速运动的方法
2013/05/08 Javascript
Node.js中使用事件发射器模式实现事件绑定详解
2014/08/15 Javascript
get(0).tagName获得作用标签示例代码
2014/10/08 Javascript
node.js中的http.response.removeHeader方法使用说明
2014/12/14 Javascript
jQuery实现鼠标选文字发新浪微博的方法
2016/04/02 Javascript
浅析JS原型继承与类的继承
2016/04/07 Javascript
AngularJS实现分页显示数据库信息
2016/07/01 Javascript
iview中Select 选择器多选校验方法
2018/03/15 Javascript
js数据类型检测总结
2018/08/05 Javascript
Vue实现左右菜单联动实现代码
2018/08/12 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
bootstrap-table后端分页功能完整实例
2020/06/01 Javascript
[02:54]DOTA2英雄基础教程 撼地者
2014/01/14 DOTA
[49:27]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第一场
2018/04/05 DOTA
用Python中的wxPython实现最基本的浏览器功能
2015/04/14 Python
Python实现字符串逆序输出功能示例
2017/06/24 Python
使用python对多个txt文件中的数据进行筛选的方法
2019/07/10 Python
python实现画循环圆
2019/11/23 Python
基于python 将列表作为参数传入函数时的测试与理解
2020/06/05 Python
北美三大旅游网站之一:Travelocity加拿大
2016/08/20 全球购物
Pretty Green美国:英式摇滚服饰风格代表品牌之一
2019/01/23 全球购物
奶茶店创业计划书范文
2014/01/17 职场文书
2013年军训通讯稿
2014/02/05 职场文书
大学军训感言600字
2014/02/25 职场文书
诚信的演讲稿范文
2014/05/12 职场文书
公证委托书标准格式
2014/09/11 职场文书
2014年国庆晚会主持词
2014/09/19 职场文书
医院2014国庆节活动策划方案
2014/09/21 职场文书
晚会开场白和结束语
2015/05/29 职场文书
Java9新特性对HTTP2协议支持与非阻塞HTTP API
2022/03/16 Java/Android