重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇“JavaScript如何调用Java的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“JavaScript如何调用Java的方法”文章吧。
望都网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
在JavaScript中想要调用Java的方法,通过JavaScript访问servlet,再通过servlet调用Java某个类的方法。
HTML代码
图片 | 名称 | 价格 | 款式 | |
---|---|---|---|---|
"> " alt=""> | "><%=shoes.getBrand() %>/<%=shoes.getSeries() %>/<%=shoes.getName() %> | <%=shoes.getPrice() %> | <%=shoes.getSex() %>/<%=shoes.getSize() %> |
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="+shoes_id+"&user_id="+22; alert("删除成功!"); } } else { alert('请选择商品!'); } getTotal(); //更新总数 }
以上就是关于“JavaScript如何调用Java的方法”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。