在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" rel="external nofollow" rel="external nofollow" >
<img src="/UploadFiles/2021-04-02/ShopServlet">
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" rel="external nofollow" +shoes_id+"&user_id="+22;
alert("删除成功!");
}
} else {
alert('请选择商品!');
}
getTotal(); //更新总数
}
以上就是JavaScript中如何调用Java方法的详细内容,更多关于js中调用Java方法的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JavaScript中如何调用Java方法”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]