新闻发布项目——Servlet类(doCategoryaddServlet)

简介: 新闻发布项目——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
  }
}


目录
相关文章
|
13天前
新闻发布项目——Servlet类(doRegServlet )
新闻发布项目——Servlet类(doRegServlet )
12 0
|
13天前
新闻发布项目——Servlet类(doNewsAddServlet)
新闻发布项目——Servlet类(doNewsAddServlet)
12 0
|
13天前
|
自然语言处理 Java 应用服务中间件
从零手写实现 tomcat-09-servlet 处理类
该文是一个关于手写实现 Apache Tomcat 简化版的系列教程摘要。作者希望通过亲自实现一个简单的 Tomcat,来深入理解其工作原理。系列教程包括了从入门介绍到解析处理 WAR 包、与 SpringBoot 集成等多个步骤。文章介绍了 Servlet 的概念,将其比作餐厅服务员,负责处理网络请求和响应。文中还详细阐述了 Servlet 的处理流程,并通过实例解释了如何实现一个基础的 Servlet。最后,提到了如何根据请求 URL 进行调度和处理,并给出了迷你版 Tomcat(Mini-Cat)的开源地址。
|
13天前
新闻发布项目——Servlet类(doNewsModifyServlet )
新闻发布项目——Servlet类(doNewsModifyServlet )
13 0
|
13天前
|
Java
springboot项目出现Exception in thread “main“ java.lang.NoClassDefFoundError: javax/servlet/Filter
springboot项目出现Exception in thread “main“ java.lang.NoClassDefFoundError: javax/servlet/Filter
19 0
|
13天前
|
Java
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
34 3
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
|
13天前
|
Java
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
24 0
|
13天前
|
Java
排课系统【JSP+Servlet+JavaBean】(Java课设)
排课系统【JSP+Servlet+JavaBean】(Java课设)
28 5
|
13天前
|
Java
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
20 1
|
13天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
23 2