欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript中如何调用Java方法

程序员文章站 2022-03-30 17:53:22
在javascript中想要调用java的方法,我通过javascript访问servlet,再通过servlet调用java某个类的方法。html代码

在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方法的资料请关注其它相关文章!