黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 JavaScript調(diào)用Java的方法

JavaScript調(diào)用Java的方法

更新時(shí)間:2021-06-09 09:56:50 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1052次

在JavaScript中想要調(diào)用Java的方法,通過JavaScript訪問servlet,再通過servlet調(diào)用Java某個(gè)類的方法。

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:;"><i class="fa fa-trash-o"></i></a>
            </th>
            <th class="product_thumb">圖片</th>
            <th class="product_name">名稱</th>
            <th class="product-price">價(jià)格</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() %>">
                    <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() %>"><%=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代碼

// 點(diǎn)擊全部刪除
    deleteAll.onclick = function () {
        if (selectedTotal.innerHTML != 0) {
            var con = confirm('確定刪除所選商品嗎?'); //彈出確認(rèn)框
            if (con) {
                var shoes_id = '';
                for (var i = 0; i < tr.length; i++) {
                    // 如果被選中,就刪除相應(yīng)的行
                    if (tr[i].getElementsByTagName('input')[0].checked) {
                        shoes_id = shoes_id + tr[i].cells[0].innerHTML + ',';                        
                        //tr[i].parentNode.removeChild(tr[i]); // 刪除相應(yīng)節(jié)點(diǎn)
                        //i--; //回退下標(biāo)位置
                    }
                }
                alert(shoes_id);
                window.location.href="ShopServlet?method=deleteFromCart&shoes_id="+shoes_id+"&user_id="+22;
                alert("刪除成功!");
            }
        } else {
            alert('請選擇商品!');
        }
        getTotal(); //更新總數(shù)
    }

以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"JavaScript調(diào)用Java的方法",希望對大家有幫助,如有疑問,請?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您服務(wù)。

提交申請后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产日本在线播放 | 欧美日韩高清观看一区二区 | 国产短视频精品一区二区三区 | jizzjizz日本老师 | 91久久国产青草亚洲 | 日韩一区二区三 | 成人毛片免费观看视频在线 | 天天躁| 国产精品久久人人做人人爽 | 美国一级做a爰片性色毛片 美国一级做a一级视频 | 亚色影视| 免费黄色成人 | 日本不卡视频网站 | 日本大片久久久高清免费看 | 污视频黄| 在线观看男女爱视频网站 | 99精品在免费线视频 | 国产免费又爽又黄又刺激的视频 | 国产大片中文字幕 | 久久丝袜精品中文字幕 | 最近中文字幕大全2019 | 国产精品不卡视频 | 国产人人艹| 国产精选莉莉私人影院 | 日韩在线视频免费播放 | 午夜免费福利网站 | 久久黄色网址 | 久久综合五月天 | 视频一区二区三区在线观看 | 色一级| 亚洲人成毛片线播放 | 精品国产国语对白主播野战 | 久久久久亚洲视频 | 国产福利微拍精品一区二区 | 免费观看黄a一级视频日本 免费观看黄a一级视频 | 精品欧美高清不卡在线 | 18以下勿进色禁网站 | 日韩欧美视频一区 | 禁网站在线观看免费视频 | v视界影院视频一区二区三区 | 人人澡人人人人夜夜爽 |