jsp中表單頁面
<span style="font-size:12px;"><form action="<%= request.getContextPath() %>/DelectServlet" method=post> <tr> <th> 全選/反選<input type="checkbox" id="checkall" name="checkall" onclick="checkAll(checkall)" /><br> <input type="submit" value="刪除" onclick="return confirm('確定刪除選擇項。')" align="left"> <input type="button" value="修改" onclick="doDelete()" align="left"> </th> <th>編號</th> <th>使用者名稱</th> <th>密碼</th> <th>姓名</th> <th>郵箱</th> <th>性別</th> <th>生日</th> <th>愛好</th> <th>薪水</th> <th>其他</th> <th>角色</th> </tr> <c:forEach var="person" items="${jsp}"> <tr> <td align ="center"><input type="checkbox" id=' ${person.id}' name='info' value= '${person.id}' /></td> <td>${person.id}</td> <td>${person.username}</td> <td>${person.passwd}</td> <td>${person.realname}</td> <td>${person.email}</td> <td>${person.sex}</td> <td>${person.birth}</td> <td>${person.habit}</td> <td>${person.salary}</td> <td>${person.other}</td> <td>${person.role}</td> </tr> </c:forEach> </form></span>
js實現全選反選功能
<script type="text/javascript">function checkAll(checkall) { arr = document.getElementsByName('info' ); if (checkall.checked == true) { for(i=0;i<arr.length;i++){ arr[i].checked = true; }}else{for(i=0;i<arr.length;i++){ if((arr[i]).checked==false){arr[i].checked = true;}else{arr[i].checked = false; }}}} </script>
實現選中刪除的servlet類
package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import Dao.Dao;import Dao.DaoJDBCImpl;/** * Servlet implementation class DelectServlet */public class DelectServlet extends HttpServlet {private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); String[] selectdelete = request.getParameterValues("info"); String ids =""; for(int i=0;i<selectdelete.length;i++){ ids += "'"+selectdelete[i]+"'"; if(i!=selectdelete.length-1) ids+=","; try {Dao dao = new DaoJDBCImpl();dao.remove(ids);out.print("<script language='javascript'>alert('刪除成功。');window.location.href='ShowServlet';</script>");out.flush();out.close();} catch (Exception e) {// TODO Auto-generated catch block} } }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }}
jdbc資料庫刪除資料
//刪除人員 public void remove(String id) throws Exception{ dbc = new DaoJDBCImpl(); String sql="delete from person where id in ("+id+")"; PreparedStatement pst = dbc.getConnection().prepareStatement(sql); pst.executeUpdate(); pst.close(); dbc.close(); }