資源描述:
《Servlet增刪改查案例經(jīng)典實現(xiàn)》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
1、AddpublicclassAddextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//編碼問題response.setContentType("text/html;charset=GBK");//瀏覽器顯示的編碼,默認的是isoresponse.setCharacterEncoding("GBK");//瀏覽器到數(shù)據(jù)庫取數(shù)據(jù)時候使用的編碼reques
2、t.setCharacterEncoding("GBK");//瀏覽器到數(shù)據(jù)庫存入數(shù)據(jù)時候所使用的編碼方式//必須為post方式才行的//獲取所提交表單的值Stringid=request.getParameter("id");//嚴謹?shù)木幋a:if("".equals(id)
3、
4、id==null){response.sendRedirect("./Main.html");//System.out.println("zzz");return;}Stringname=request.getParameter("user");//Stringname=
5、newString(name1.getBytes(("GBK")));//"ISO-8859-1",intuserage=Integer.parseInt(request.getParameter("age"));Stringaddr=request.getParameter("address");intuserid=Integer.parseInt(id);//調(diào)用dao層的方法進行添加到數(shù)據(jù)庫的操作Personp=newPerson();p.setId(userid);p.setName(name);p.setAge(userage);p.s
6、etAddress(addr);IPersonDaoperson=newPersonDaoImpl();person.addPerson(p);//添加過后則重定向,跳向查詢?nèi)康捻撁鎟esponse.sendRedirect("/Jsp/selectAll");}//Post方法處理post的請求publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,respon
7、se);}}DeletepublicclassDeleteextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{IPersonDaoperson=newPersonDaoImpl();intid=Integer.parseInt(request.getParameter("id").trim());person.delPersonById(id);resp
8、onse.sendRedirect("/Jsp/selectAll");//重定向查詢?nèi)宽撁?/RequestDispatcherdp=request.getRequestDispatcher("/Add/selectAll");//轉(zhuǎn)發(fā)方式//dp.forward(request,response);}}publicclassDelAllextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletEx
9、ception,IOException{//獲取多個box的值的方法String[]s=request.getParameterValues("like");//?IPersonDaoperson=newPersonDaoImpl();//使用for循環(huán)進行刪除操作for(inti=0;i10、d/selectAll");//跳到查詢?nèi)宽撁鎪//Post方法處理post的請求publicvoiddoPost(HttpServl