首页>代码>基于servlet+bootstrap+mysql的java轻博客系统>/blog(EL版本)/blog/src/cap/action/AdminServlet.java
package cap.action;

import java.io.IOException;
import java.util.List;
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 javax.servlet.http.HttpSession;
import cap.bean.Admin;
import cap.bean.Article;
import cap.bean.Comment;
import cap.bean.Counter;
import cap.bean.Profile;
import cap.bean.SysCategory;
import cap.bean.User;
import cap.service.AdminService;
import cap.service.ArticleService;
import cap.service.CommentService;
import cap.service.CounterService;
import cap.service.ProfileService;
import cap.service.SysCategoryService;
import cap.service.UserService;
import cap.service.impl.AdminServiceImpl;
import cap.service.impl.ArticleServiceImpl;
import cap.service.impl.CommentServiceImpl;
import cap.service.impl.CounterServiceImpl;
import cap.service.impl.ProfileServiceImpl;
import cap.service.impl.SysCategoryServiceImpl;
import cap.service.impl.UserServiceImpl;
import cap.util.PageControl;


@WebServlet("/admin.html")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	private UserService userService;
	private ProfileService profileService;
	private ArticleService artService;
	private SysCategoryService scService;
	private CounterService cntService;
	private CommentService cmtService;
       
    
    public AdminServlet() {
    	adminService=new AdminServiceImpl();
    	userService=new UserServiceImpl();
    	profileService=new ProfileServiceImpl();
    	artService=new ArticleServiceImpl();
    	scService=new SysCategoryServiceImpl();
    	cntService=new CounterServiceImpl();
    	cmtService=new CommentServiceImpl();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if(action.equals("login")){
			String userName = request.getParameter("username");
			String passWord = request.getParameter("password");

			Admin admin = adminService.login(userName, passWord);
			
			if (admin == null) {
				request.getSession().setAttribute("msg", "用户名或密码不正确!");
				response.sendRedirect("AdminLogin.jsp");
			} else {
				request.getSession().setAttribute("admin", admin);
				response.sendRedirect("admin.html?action=index");
			}
			
		}else if(action.equals("logout")){
			HttpSession session = request.getSession(false);    //防止创建Session
			
			if (null != session) {
				session.removeAttribute("admin");
			}
			
			request.getSession().setAttribute("logoutMsg", "退出成功!");
			response.sendRedirect("AdminLogin.jsp");
			
		}else if(action.equals("index")){			
			List<Article> artList = artService.getAllArtical(); //获取所有文章		
			List<Comment> cmtList = cmtService.getAll(); //获取所有评论	
			List<User> uList = userService.getAllUser(); //获取所有用户
			Counter cnt = cntService.getCounter();
			request.setAttribute("artList", artList);
			request.setAttribute("cmtList", cmtList);
			request.setAttribute("uList", uList);
			request.setAttribute("cnt", cnt);
			request.getRequestDispatcher("admin/Index.jsp").forward(request, response);
		}	
		else if(action.equals("useradmin")){			
			//分页
			String curPageStr = request.getParameter("curPage");
			PageControl pc=userService.getUserOfPage(curPageStr);
			request.setAttribute("curPage", pc.getCurPage());
			request.setAttribute("totalPages", pc.getTotalPages());
			
			request.setAttribute("uList", pc.getList());
			request.getRequestDispatcher("admin/UserAdmin.jsp").forward(request, response);
		}else if(action.equals("userprofile")){
				
			int userId = Integer.parseInt(request.getParameter("uId"));
			Profile pf = profileService.getByuserId(userId);			
			request.setAttribute("pf", pf);
			request.getRequestDispatcher("/admin/UserProfile.jsp").forward(request, response);
			
		}else if(action.equals("deleteuser")){
			int uId = Integer.parseInt(request.getParameter("uId"));
			int res = userService.deleteUser(uId);
			
			if (res > 0) {	//删除成功
				request.getSession().setAttribute("succDeleMsg", "禁用账户成功!");
			} else {     //删除失败
				request.getSession().setAttribute("errorDeleMsg", "禁用账户失败!");
			}
			
			response.sendRedirect("admin.html?action=useradmin");
		}else if(action.equals("activeuser")){
			
			int uId = Integer.parseInt(request.getParameter("uId"));
			int res = userService.activeUser(uId);
			
			if (res > 0) {	//激活成功
				request.getSession().setAttribute("succActMsg", "激活账户成功!");
			} else {     //激活失败
				request.getSession().setAttribute("errorActMsg", "激活账户失败!");
			}			
			response.sendRedirect("admin.html?action=useradmin");
			
		}else if(action.equals("SysArticalAdmin")){

			String curPageStr = request.getParameter("curPage");
			PageControl pc = artService.getData(curPageStr);
			request.setAttribute("curPage", pc.getCurPage());
			request.setAttribute("totalPages", pc.getTotalPages());
			List<Article> artList=pc.getList();
			request.setAttribute("artList", artList);
			request.getRequestDispatcher("admin/SysArticleAdmin.jsp").forward(request, response);
			
		}else if(action.equals("DeleteSysArtical")){
			int artId = Integer.parseInt(request.getParameter("artId"));
			int res = artService.deleteArtical(artId);
			
			if (res > 0) {	//删除成功
				request.getSession().setAttribute("deleSuccMsg", "删除成功!");
			} else {
				request.getSession().setAttribute("deleErrorMsg", "删除失败!");
			}
			
			response.sendRedirect("admin.html?action=SysArticalAdmin");
			
			
		}else if(action.equals("SysCategoryAdmin")){

			String curPageStr = request.getParameter("curPage");
			PageControl pc=scService.getSysCategoryByPage(curPageStr);
			request.setAttribute("scgList", pc.getList());
			request.setAttribute("curPage", pc.getCurPage());
			request.setAttribute("totalPages", pc.getTotalPages());
			request.getRequestDispatcher("admin/SysCategoryAdmin.jsp").forward(request, response);
			
			
		}else if(action.equals("EditSysCategory")){
			int scgId = Integer.parseInt(request.getParameter("scgId"));
			SysCategory scg = scService.getById(scgId);			
			request.setAttribute("scg", scg);
			request.getRequestDispatcher("admin/EditSysCategory.jsp").forward(request, response);
			
			
		}else if(action.equals("SaveEditSysCategory")){
			int scgId = Integer.parseInt(request.getParameter("scgId"));			
			String scgName = request.getParameter("scgName");
			SysCategory scg = scService.getByName(scgName);
			
			if ((null != scg) && (scgId != scg.getId())) {
				request.getSession().setAttribute("scgExist", "该分类名已存在,请重新输入分类名!");
				response.sendRedirect("EditSysCategory.html?scgId="+scgId);
				
			} else {			//允许更新该分类名
				int res = scService.updateSysCategory(scgId, scgName);
				
				if (res > 0) {	//更新成功
					request.getSession().setAttribute("succUpdateScg", "更新分类成功!");
					response.sendRedirect("admin.html?action=SysCategoryAdmin");
				} else {
					request.getSession().setAttribute("errorUpdateScg", "更新分类失败!");
					response.sendRedirect("admin.html?action=SysCategoryAdmin");
				}
			}
		}else if(action.equals("AddSysCategory")){
			String scgName = request.getParameter("scgName");
			SysCategory scg = scService.getByName(scgName);
			
			if (null != scg) {    //提示该分类已存在
				request.getSession().setAttribute("scgExist", "该分类名已存在,请重新输入分类名!");
				response.sendRedirect("admin/AddSysCategory.jsp");
				
			} else {
				int res = scService.insertSysCategory(scgName);
				
				if (res > 0) {	  //添加分类成功
					request.getSession().setAttribute("succAddScg", "添加分类成功!");
				} else {		  //添加分类失败
					request.getSession().setAttribute("errorAddScg", "添加分类失败!");
				}
				
				response.sendRedirect("admin.html?action=SysCategoryAdmin");
			}
		}else if(action.equals("DeleteSysCategory")){
			int scgId = Integer.parseInt(request.getParameter("scgId"));
			//SysCategoryDao scgDao = new SysCategoryDao();
			int res = scService.deleteSysCategory(scgId);
			
			if (res > 0) {
				request.getSession().setAttribute("succDeleScg", "删除分类成功!");
			} else {
				request.getSession().setAttribute("errorDeleScg", "删除分类失败!");
			}
			
			response.sendRedirect("admin.html?action=SysCategoryAdmin");
		}
	}

	

	
}
最近下载更多
aono47  LV1 6月17日
liangge2115  LV27 5月15日
1140598224  LV1 2月1日
天寒雨落  LV1 2023年6月16日
benyuan  LV1 2022年12月25日
upup996  LV6 2022年12月18日
你们的代码都是我的了  LV16 2022年12月5日
SQ2930501923  LV14 2022年10月10日
陈小灏  LV15 2022年6月14日
wanglinddad  LV55 2022年5月12日
最近浏览更多
aono47  LV1 6月17日
liangge2115  LV27 5月15日
yxzzxy  LV3 2月18日
1140598224  LV1 2月1日
赵明磊 1月18日
暂无贡献等级
abandan  LV4 2023年11月6日
zhaobing_g  LV3 2023年8月28日
周敏国  LV9 2023年8月21日
天寒雨落  LV1 2023年6月16日
wanglongfei2  LV2 2023年6月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友