/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package servlet; import dao.NewsRealeseDao; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InsertOneNews extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { ArrayList addnews_list = new ArrayList(); NewsRealeseDao newsRealeseDao = new NewsRealeseDao(); addnews_list.add(0, request.getParameter("newsid")); addnews_list.add(1, request.getParameter("classid")); addnews_list.add(2, request.getParameter("kindid")); addnews_list.add(3, request.getParameter("myother")); addnews_list.add(4, request.getParameter("headtitle")); addnews_list.add(5, request.getParameter("content")); addnews_list.add(6, request.getParameter("connectrealtive")); addnews_list.add(7, request.getParameter("author")); addnews_list.add(8, request.getParameter("editor")); addnews_list.add(9, request.getParameter("newsfrom")); addnews_list.add(10, request.getParameter("top")); addnews_list.add(11, request.getParameter("newstime")); addnews_list.add(12, request.getParameter("hits")); addnews_list.add(13, request.getParameter("state")); addnews_list.add(14,request.getParameter("tag")); try { String newsRealese = newsRealeseDao.insertOneNews(addnews_list); System.out.println("String:"+newsRealese); if (newsRealese!= null) { response.sendRedirect("adminManager.jsp"); } else { response.sendRedirect("https://www.jb51.net/a_addNews.jsp"); } } catch (Exception ex) { Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex); } } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; } }
至此,我们完整的实现了插入工作 (7)现在完成删除操作,在Servlet包中建立如下所示代码,命名为DeleteOneNews.java:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package servlet; import dao.NewsRealeseDao; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DeleteOneNews extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String str_id=request.getParameter("newsid"); int newsid_int=Integer.valueOf(str_id); NewsRealeseDao newsRealeseDao=new NewsRealeseDao(); try { String newsRealese = newsRealeseDao.deleteOneNews(newsid_int); System.out.println("String:"+newsRealese); if (newsRealese!= null) { response.sendRedirect("adminManager.jsp"); } else { response.sendRedirect("https://www.jb51.net/a_addNews.jsp"); } } catch (Exception ex) { Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex); } } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
(8)实现查询操作,命名为a_queryNews.jsp,其代码如下所示: