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 相关文章推荐
使用JSLint提高JS代码质量方法分享
Dec 16 Javascript
Javascript和Java获取各种form表单信息的简单实例
Feb 14 Javascript
js代码实现的加入收藏效果并兼容主流浏览器
Jun 23 Javascript
JS动态修改iframe内嵌网页地址的方法
Apr 01 Javascript
JavaScript中使用指数方法Math.exp()的简介
Jun 15 Javascript
javascript中setAttribute()函数使用方法及兼容性
Jul 19 Javascript
纯原生js实现table表格的增删
Jan 05 Javascript
jQuery遮罩层实例讲解
May 11 jQuery
vue自定义过滤器创建和使用方法详解
Nov 06 Javascript
bootstrap实现二级下拉菜单效果
Nov 23 Javascript
JS中的算法与数据结构之集合(Set)实例详解
Aug 20 Javascript
element日历calendar组件上月、今天、下月、日历块点击事件及模板源码
Jul 27 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
Content-type 的说明
2006/10/09 PHP
十天学会php之第七天
2006/10/09 PHP
PHP函数utf8转gb2312编码
2006/12/21 PHP
PHP生成树的方法
2015/07/28 PHP
yii2使用ajax返回json的实现方法
2016/05/14 PHP
Zend Framework框架中实现Ajax的方法示例
2017/06/27 PHP
SCP远程VPS快速搬家和WDCP升级php5.3安装memcached和eaccelerator教程
2017/07/27 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
一些有关检查数据的JS代码
2006/09/07 Javascript
from表单多个按钮提交用onclick跳转不同action
2014/04/24 Javascript
概述javascript在Google IE中的调试技巧
2016/11/24 Javascript
JS轮播图中缓动函数的封装
2020/11/25 Javascript
bootstrap警告框示例代码分享
2017/05/17 Javascript
js实现加载页面就自动触发超链接的示例
2017/08/31 Javascript
JS匿名函数和匿名自执行函数概念与用法分析
2018/03/16 Javascript
Python2和Python3中print的用法示例总结
2017/10/25 Python
python让列表倒序输出的实例
2018/06/25 Python
python抓取京东小米8手机配置信息
2018/11/13 Python
详解python项目实战:模拟登陆CSDN
2019/04/04 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
NumPy统计函数的实现方法
2020/01/21 Python
Pandas DataFrame求差集的示例代码
2020/12/13 Python
html5简介及新增功能介绍
2020/05/18 HTML / CSS
毕业生简单求职信
2013/11/19 职场文书
高中自我评价范文
2014/01/27 职场文书
求职信模板标准格式范文
2014/02/23 职场文书
服务承诺书怎么写
2014/05/24 职场文书
党的群众路线教育实践活动心得体会(医院)
2014/11/03 职场文书
2014年小学工作总结
2014/11/26 职场文书
学生考试舞弊检讨书
2015/01/01 职场文书
九华山导游词
2015/02/03 职场文书
生死牛玉儒观后感
2015/06/11 职场文书
工作简报范文
2015/07/21 职场文书
2015年政治教研组工作总结
2015/07/22 职场文书
Java实现聊天机器人完善版
2021/07/04 Java/Android
vue里使用create, mounted调用方法
2022/04/26 Vue.js