zoukankan      html  css  js  c++  java
  • 新闻发布项目——Servlet类(doCategoryaddServlet)

    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
    	}
    
    }
    

  • 相关阅读:
    P2015 二叉苹果树(树形DP)
    Treats for the Cows (区间DP)
    You Are the One(区间DP 好题)
    Palindrome subsequence (区间DP)
    Cutting Sticks(区间DP)
    L2-013 红色警报 (dfs判断图连通性)
    L2-001 紧急救援 (dijkstra+dfs回溯路径)
    多线程 -- JMM、volatile关键字、内存屏障、happens-before原则、缓存一致性
    多线程 -- 各种锁的概念
    Spring Boot 学习笔记(十六)启动原理、运行流程、自动配置原理
  • 原文地址:https://www.cnblogs.com/a1111/p/12816515.html
Copyright © 2011-2022 走看看