博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记账本程序四
阅读量:5254 次
发布时间:2019-06-14

本文共 6943 字,大约阅读时间需要 23 分钟。

 

代码如下

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");                List
jizhang = 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;    }}

运行程序

 

 

 

转载于:https://www.cnblogs.com/lishengming00/p/10446635.html

你可能感兴趣的文章
今天一天课,随便写点吧/xk
查看>>
python常见面试题
查看>>
vscode调试运行c#
查看>>
VBS-Dictionary
查看>>
[android] 让原声android嵌入H5 支持localStorage
查看>>
搭建sqlserver读写分离 需要准备什么,怎么做?
查看>>
入门图像处理
查看>>
Acrobat.CAcroPDDoc open 无法找到指定文件
查看>>
Google css & Google fonts
查看>>
你是码农涅,还是创造者涅(内容是转载的,标题是我写的)
查看>>
如何组织Html元素与如何进行CSS命名(下)
查看>>
大型物件列印之翹曲現象與加熱板作用論述
查看>>
element input-number源码
查看>>
使用WiX打包你的应用程序之二---向WiX脚本传递信息(属性)的几种方式
查看>>
使用this 调用另一个构造器
查看>>
Input checkbox 添加样式背景
查看>>
(C语言)单链表的顺序实现(数据结构一)
查看>>
shuzhuanshuzu
查看>>
poj3384Feng Shui
查看>>
neural network
查看>>