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

  • 相关阅读:
    线程同步的三种方式(Mutex,Event,Critical Section) 沧海
    VC++多线程下内存操作的优化 沧海
    C++内存对象大会战 沧海
    技术关注:搜索引擎经验 沧海
    jira 3.13.5版 安装 配置 用户权限控制 拂晓风起
    C++ int string 转换 拂晓风起
    C++调用C链接库会出现的问题 拂晓风起
    Windows Server 2003 IIS Service Unavailable 问题解决 拂晓风起
    研究 学术 开发 的好用工具(不包括常见的) 拂晓风起
    SGMarks 问世 (Firefox扩展Gmarks的扩展版) 纯属学习 拂晓风起
  • 原文地址:https://www.cnblogs.com/a1111/p/12816515.html
Copyright © 2011-2022 走看看