package bdqn.newsManageServlet.Servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bdqn.newsManageServlet.Service.categoryTBService; import bdqn.newsManageServlet.Service.Impl.categoryTBServiceImpl; import bdqn.newsManageServlet.entity.categoryTB; public class doCategoryaddServlet extends HttpServlet { /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); String name=request.getParameter("categoryName"); if(name==null||name.equals("")){ request.setAttribute("ermsg", "类别名称不能为空!"); request.getRequestDispatcher("addCategory.jsp").forward(request, response); return; } //调用访问数据称 //categoryTBDao cdao=new categoryTBDaoImpl(); categoryTBService cgService=new categoryTBServiceImpl(); //1.判断类别是否重复(在数据库中查询) //2.如果有重复,提示错误信息,转发到添加 if(cgService.getCateByName(name)!=null){ request.setAttribute("ermsg", "类别名已存在!"); request.getRequestDispatcher("adminManage/addCategory.jsp").forward(request, response); return; } //3.不重复,实现添加功能,提示添加成功,跳转到管理页面 //先封装实体类 categoryTB cate=new categoryTB(); cate.setCategoryName(name); int rel=cgService.addCategory(cate); if(rel>0){ //添加成功 out.print("<script>alert('添加成功');location.href='adminManage/manageTopic.jsp';</script>"); }else{ request.setAttribute("ermsg", "添加失败"); request.getRequestDispatcher("adminManage/addCategory.jsp").forward(request, response); return; } } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }