• 2020.12.9收获


    CourseServlet.java

    package servlet;
    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 entity.Course;
    import dao.CourseDao;
    @WebServlet("/CourseServlet")
    public class CourseServlet extends HttpServlet {
        
        private static final long serialVersionUID = 1L;
         public CourseServlet() {
                super();
                // TODO Auto-generated constructor stub
            }
        CourseDao dao = new CourseDao();
        
        
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method = req.getParameter("method");
            if ("add".equals(method)) {
                add(req, resp);
            } else if ("del".equals(method)) {
                del(req, resp);
            } else if ("update".equals(method)) {
                update(req, resp);
            } else if ("search".equals(method)) {
                search(req, resp);
            } else if ("getCourseById".equals(method)) {
                getCourseById(req, resp);
            } else if ("getcoursebyname".equals(method)) {
                getCourseByName(req, resp);
            } else if ("list".equals(method)) {
                list(req, resp);
            }
            else if ("searchlist".equals(method)) {
                searchlist(req, resp);
            }
        }
    
        
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String minzu = req.getParameter("minzu");
            String time = req.getParameter("time");
            String age = req.getParameter("age");
            String mianmao = req.getParameter("mianmao");
            String fuwu = req.getParameter("fuwu");
            Course course = new Course(name, sex, minzu, time,age,mianmao,fuwu);
            Course course1 = dao.getCourseByName(name);
            
            if(course1==null) {
                dao.add(course);
                req.setAttribute("message", "添加成功");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            } else {
                req.setAttribute("message", "失败:姓名重复");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            }
        }
        
        
        private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            List<Course> courses = dao.list();
            req.setAttribute("courses", courses);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }
        private void searchlist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            List<Course> courses = dao.list();
            req.setAttribute("courses", courses);
            req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
        }
    
        
        private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("id"));
            Course course = dao.getCourseById(id);
            req.setAttribute("course", course);
            req.getRequestDispatcher("detail2.jsp").forward(req,resp);
        }
    
        
        private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            Course course = dao.getCourseByName(name);
            if(course == null) {
                req.setAttribute("message", "无此志愿者");
                req.getRequestDispatcher("del.jsp").forward(req,resp);
            } else {
                req.setAttribute("course", course);
                req.getRequestDispatcher("detail.jsp").forward(req,resp);
            }
        }
        
        
        private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("id"));
            dao.delete(id);
            req.setAttribute("message", "删除成功");
            req.getRequestDispatcher("del.jsp").forward(req,resp);
        }
        
        
        private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String minzu = req.getParameter("minzu");
            String time = req.getParameter("time");
            String age = req.getParameter("age");
            String mianmao = req.getParameter("mianmao");
            String fuwu = req.getParameter("fuwu");
            Course course = new Course(id,name, sex, minzu, time,age,mianmao,fuwu);
            
            dao.update(course);
            req.setAttribute("message", "修改成功");
            req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp);
        }
        
        
        private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String minzu = req.getParameter("minzu");
            String time = req.getParameter("time");
            String age = req.getParameter("age");
            String mianmao = req.getParameter("mianmao");
            String fuwu = req.getParameter("fuwu");
            List<Course> courses = dao.search(name, sex, minzu, time,age,mianmao,fuwu);
            Course course1 = dao.getCourseByName(name);
            if(course1 == null) {
                req.setAttribute("message", "无此志愿者");
                req.getRequestDispatcher("search.jsp").forward(req,resp);
            } else {
                req.setAttribute("courses", courses);
                req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
            }
        }
    }
  • 相关阅读:
    ASP.NET程序中常用的三十三种代码
    uri
    用XML保存和读取图片
    location
    访问相关节点
    onkeyup,onkeydown,onkeypress
    Asp.net中Server.Transfer,Server.Execute和Response.Redirect的区别
    关于window.showModalDialog()返回值的学习心得
    WP7数据绑定
    hdu 1568
  • 原文地址:https://www.cnblogs.com/ltw222/p/14171094.html
Copyright © 2020-2023  润新知