com.jsj.web.manage====新闻发布系统(11)
2020-05-15
这个是管理员的Servlet
ManageServlet
1 package com.jsj.web.manage; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebServlet("/admin/manage") 11 public class ManageServlet extends HttpServlet { 12 13 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 request.getRequestDispatcher("/WEB-INF/view/admin/manage/index.jsp").forward(request,response); 15 } 16 }
NewsCateServlet
1 package com.jsj.web.manage; 2 3 import com.jsj.entity.NewsCate; 4 import com.jsj.factory.ServiceFactory; 5 import com.jsj.service.NewsCateService; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import java.io.IOException; 13 import java.io.PrintWriter; 14 import java.util.List; 15 16 @WebServlet("/admin/manage/newsCate") 17 public class NewsCateServlet extends HttpServlet { 18 19 private NewsCateService newsCateService = (NewsCateService) ServiceFactory.getService("NewsCateService"); 20 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 List<NewsCate> allCate = newsCateService.getAllCate(); 23 request.setAttribute("allCate",allCate); 24 request.getRequestDispatcher("/WEB-INF/view/admin/manage/newsCate.jsp").forward(request,response); 25 } 26 27 @Override 28 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 29 response.setContentType("text/html;charset=UTF-8"); 30 try(PrintWriter out = response.getWriter()){ 31 if (request.getParameter("method")!=null&&request.getParameter("method").equals("insert")){ 32 //新增分类 33 insert(request,out); 34 }else if (request.getParameter("name")!=null){ 35 //修改分类 36 update(request,out); 37 }else { 38 //删除分类 39 delete(request, out); 40 } 41 response.setHeader("refresh", "2;url=/admin/manage/newsCate"); 42 } 43 } 44 45 /** 46 * 添加分类 47 */ 48 private void insert(HttpServletRequest request,PrintWriter out) { 49 String name = request.getParameter("name"); 50 NewsCate newsCate = new NewsCate(); 51 newsCate.setName(name); 52 int res = newsCateService.insert(newsCate); 53 if (res>0){ 54 out.println("添加成功"); 55 }else { 56 out.println("添加失败"); 57 } 58 } 59 60 /** 61 * 删除分类 62 */ 63 private void delete(HttpServletRequest request, PrintWriter out) { 64 Integer id = Integer.valueOf(request.getParameter("id")); 65 int res = newsCateService.delete(id); 66 if (res>0){ 67 out.println("删除成功"); 68 }else { 69 out.println("添加失败"); 70 } 71 } 72 73 /** 74 * 修改分类 75 */ 76 private void update(HttpServletRequest request,PrintWriter out) { 77 String name = request.getParameter("name"); 78 Integer id = Integer.valueOf(request.getParameter("id")); 79 NewsCate newsCate = new NewsCate(); 80 newsCate.setId(id); 81 newsCate.setName(name); 82 int res = newsCateService.update(newsCate); 83 if (res>0){ 84 out.println("修改成功"); 85 }else { 86 out.println("修改失败"); 87 } 88 } 89 90 }
NewsServlet
1 package com.jsj.web.manage; 2 3 import com.jsj.entity.NewsVo; 4 import com.jsj.entity.Page; 5 import com.jsj.factory.ServiceFactory; 6 import com.jsj.service.NewsService; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import java.io.IOException; 14 import java.io.PrintWriter; 15 16 @WebServlet("/admin/manage/news") 17 public class NewsServlet extends HttpServlet { 18 19 private NewsService newsService = (NewsService) ServiceFactory.getService("NewsService"); 20 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 try{ 23 int pageIndex = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request.getParameter("pageIndex")); 24 int pageSize = 10; 25 Page<NewsVo> newsVoPage = newsService.getNewsVoPage(pageIndex,pageSize); 26 if (newsVoPage.getBeanList() == null || newsVoPage.getBeanList().size() == 0){ 27 request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response); 28 }else { 29 request.setAttribute("newsVoPage", newsVoPage); 30 request.getRequestDispatcher("/WEB-INF/view/admin/manage/news.jsp").forward(request,response); 31 } 32 }catch (Exception e){ 33 request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response); 34 } 35 36 } 37 38 /** 39 * 删除新闻文章 40 */ 41 @Override 42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 43 response.setContentType("text/html;charset=UTF-8"); 44 try(PrintWriter out = response.getWriter()){ 45 Integer id = Integer.valueOf(request.getParameter("id")); 46 int res = newsService.deleteNewsById(id); 47 if (res>0){ 48 out.println("新闻删除成功"); 49 }else { 50 out.println("新闻删除失败"); 51 } 52 response.setHeader("refresh", "2;url=/admin/manage/news"); 53 } 54 } 55 56 57 }
PublishNewsServlet
1 package com.jsj.web.manage; 2 3 import com.jsj.entity.Admin; 4 import com.jsj.entity.News; 5 import com.jsj.entity.NewsCate; 6 import com.jsj.factory.ServiceFactory; 7 import com.jsj.service.NewsCateService; 8 import com.jsj.service.NewsService; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.annotation.WebServlet; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 import java.io.IOException; 16 import java.io.PrintWriter; 17 import java.util.Date; 18 import java.util.List; 19 20 @WebServlet("/admin/manage/publish") 21 public class PublishNewsServlet extends HttpServlet { 22 23 private NewsCateService newsCateService = (NewsCateService) ServiceFactory.getService("NewsCateService"); 24 25 private NewsService newsService = (NewsService) ServiceFactory.getService("NewsService"); 26 27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 28 List<NewsCate> allCate = newsCateService.getAllCate(); 29 request.setAttribute("allCate",allCate); 30 request.getRequestDispatcher("/WEB-INF/view/admin/manage/publish.jsp").forward(request,response); 31 } 32 33 /** 34 * 发表文章 35 */ 36 @Override 37 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 38 response.setContentType("text/html;charset=UTF-8"); 39 try(PrintWriter out = response.getWriter()){ 40 41 Admin admin = (Admin) request.getSession().getAttribute("admin"); 42 News news = new News(); 43 //获取新闻的标题 44 news.setTitle(request.getParameter("title")); 45 //获取作者的信息 46 news.setAuthor(admin.getUsername()); 47 //获取cateid,转换类型为int形 48 news.setCateId(Integer.valueOf(request.getParameter("cate"))); 49 //new Date()会将当前时间设置为初始值 50 news.setTime(new Date()); 51 //获取新闻内容 52 news.setContent(request.getParameter("content")); 53 //res是个标记,主要是用来表示是否成功 54 int res = newsService.publish(news); 55 56 if (res>0){ 57 out.println("新闻发表成功"); 58 }else { 59 out.println("新闻发表失败"); 60 } 61 response.setHeader("refresh", "2;url=/admin/manage"); 62 } 63 } 64 65 }
会当凌绝顶,一览众山小