JavaScript에서 Java 메서드 호출 방법
6521 단어 JavaScript호출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 방법을 호출하는 방법에 대한 더 많은 자료는 저희 다른 관련 글을 주목해 주십시오!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.