Servlet 入门—小案例1

servlet 入门案例打算做一个用户管理系统,主要核心功能就是用户的增删改查。假设你学完了java 基础,具备 jdbc 的技术能力。下面开干:

 第一步 准备工作

定义实体,简单起见,只有三个属性。

public class User {
    
    private Integer userId;
    private String userName;
    private Date birthday;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

建库建表:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`servlet_demo` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */;

USE `servlet_demo`;

/*Table structure for table `tb_user` */

DROP TABLE IF EXISTS `tb_user`;

CREATE TABLE `tb_user` (
  `user_id` bigint(20) DEFAULT NULL,
  `username` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `birthday` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

第二步,编写 servelt

这里有个问题,servlet 要处理增删改查 4 种业务逻辑,怎么办呢,我们之前都是一个 servlet 只做一种事情。

这里有两种思路:自定义参数来标识业务类型,或者使用 servlet 自带的 http 方法,doPost、doGet、doDelete、doPut。

自定义参数,你会写很多的 if 判断。


@WebServlet(urlPatterns = {"/users"})
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        String method = req.getParameter("method");
        if ("add".equals(method)) {

        } else if ("delete".equals(method)) {

        } else if ("update".equals(method)) {

        } else if ("select".equals(method)) {

        } else {
            // 不支持的方法
            throw new ServletException("该方法不支持!");
        }

    }
}

第二种方式基本代码如下:

@WebServlet(urlPatterns = {"/users"})
public class UserRestfulServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 新增
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 删除
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 修改
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        // 查询
    }


}

好了,两种方式,任选一种,需要注意的是:

第一种方式,前端使用的都是 get 方式请求,第二种方式,前端需要分别使用不同方式请求:post、put、get、delete。

大家可以试着写一写。


如果觉得还不错的话,关注、分享、在看(关注不失联~), 原创不易,且看且珍惜~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【非典型Coder】

赏个鸡腿吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值