项目效果:
登录:
登录后的新闻列表页面:
添加新闻:
编辑新闻:
查看新闻详情:
删除标题为题目3的新闻:
思路:
将news表中的idnews设置成自动加1
下载富文本编辑器:http://ueditor.baidu.com/website/download.html
将下载得到的文件解压放入web文件夹下的yinglang文件夹中
添加新闻:
@WebServlet(name = "AddNewServlet") public class AddNewServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.sendRedirect("AddNew.jsp"); //进入新闻添加界面 } }
@WebServlet(name = "SaveAddNewServlet") public class SaveAddNewServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); News news=new News(); news.setAuthor(request.getParameter("author")); news.setTitle(request.getParameter("title")); news.setContent(request.getParameter("content")); news.setCategory(request.getParameter("category")); news.setNewsdate(Date.valueOf(request.getParameter("newsdate"))); NewsService newsService=new NewsService(); System.out.println(newsService.AddNews(news)); request.getRequestDispatcher("ShowNewsListServlet").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
删除新闻:
@WebServlet(name = "DeleteNewServlet") public class DeleteNewServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); int idnews=Integer.valueOf(request.getParameter("idnews")); System.out.println(request.getParameter("idnews")); NewsService newsService=new NewsService(); newsService.DeleteNews(idnews); request.getRequestDispatcher("ShowNewsListServlet").forward(request,response); } }
public boolean DeleteNews(int idnews){ String sql ="delete from news where idnews="+Integer.toString(idnews); System.out.println(sql); DatabaseOperation operation=new DatabaseOperation(); boolean rs=operation.executeUpdate(sql); return rs; }
修改新闻:
@WebServlet(name = "EditNewServlet") public class EditNewServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); int idnews=Integer.valueOf(request.getParameter("idnews")); NewsService newsService=new NewsService(); try { News news=newsService.GetNews(idnews); String temp=htmlToString(news.getContent()); news.setContent(temp); request.setAttribute("news",news); request.getRequestDispatcher("EditNews.jsp").forward(request,response); //将实例转发给EditNews.jsp } catch (SQLException e) { e.printStackTrace(); } } public static String htmlToString(String str) { if(str==null) return ""; if(str.equals("")) return ""; str = str.replaceAll("&", "&"); str = str.replaceAll("<", "<"); str = str.replaceAll(">", ">"); str = str.replaceAll("&amp;", "&"); str = str.replaceAll("&quot;", """); str = str.replaceAll("\"", """); str = str.replaceAll("&lt;", "<"); str = str.replaceAll("&gt;", ">"); str = str.replaceAll("&nbsp;", " "); return str; } }
查看新闻:
@WebServlet(name = "ViewNewServlet") public class ViewNewServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); int idnews=Integer.valueOf(request.getParameter("idnews")); NewsService newsService=new NewsService(); try { News news=newsService.GetNews(idnews); request.setAttribute("news",news); request.getRequestDispatcher("NewsSpecific.jsp").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } } }