package com.hanqi; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class DeleteMember */ @WebServlet("/DeleteMember") public class DeleteMember extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteMember() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); MembersDAL ud = new MembersDAL(); if(id != null && id.trim().length() > 0) { //删除 try { ud.delete(Integer.parseInt(id)); //将 id 强制转换为 int 型 response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("删除数据失败"); e.printStackTrace(); } //删除 } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class FindUserList */ @WebServlet("/FindUserList") public class FindUserList extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindUserList() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); MembersDAL ud = new MembersDAL(); try { ArrayList<Members> al = ud.getListAll(); request.setAttribute("userlist", al); } catch(Exception e) { response.getWriter().append("查找数据失败"); e.printStackTrace(); } //页面跳转 request.getRequestDispatcher("index.jsp").forward(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
原文:http://www.cnblogs.com/name-hanlin/p/5059911.html