代码如下
package com.hjf.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 com.hjf.entity.Course;import com.hjf.service.CourseService;@WebServlet("/CourseServlet")public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法选择 */ 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); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String name = req.getParameter("name"); String fenlei = req.getParameter("fenlei"); String money = req.getParameter("money"); String time = req.getParameter("time"); Course jizhang = new Course(name,fenlei,money, time); //添加后消息显示 if(service.add(jizhang)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "课程名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); Listjizhang = service.list(); req.setAttribute("courses", jizhang); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course jizhang = service.getCourseById(id); req.setAttribute("courses", jizhang); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course jizhang = service.getCourseByName(name); if(jizhang == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("courses", jizhang); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ 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 fenlei = req.getParameter("fenlei"); String money = req.getParameter("money"); String time = req.getParameter("time"); Course jizhang = new Course(name,fenlei,money, time); service.update(jizhang); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String fenlei = req.getParameter("fenlei"); String money = req.getParameter("money"); String time = req.getParameter("time"); List jizhang = service.search(name, fenlei,money, time); req.setAttribute("courses", jizhang); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); }}
接下来写了course用来给各个变量名定义
package com.hjf.entity;public class Course { private int id; private String name; private String fenlei; private String money; private String time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getfenlei() { return fenlei; } public void setfenlei(String fenlei) { this.fenlei = fenlei; } public String getmoney() { return money; } public void setmoney(String money) { this.money = money; } public String gettime() { return time; } public void settime(String time) { this.time = time; } public Course() {} public Course(int id, String name, String fenlei, String money,String time) { this.id = id; this.name = name; this.fenlei = fenlei; this.money = money; this.time=time; } public Course(String name, String fenlei, String money,String time) { this.name = name; this.fenlei = fenlei; this.money = money; this.time=time; }}
运行程序