JavaWeb课程复习资料(八)——添加功能

2022-11-29 20:18:48 浏览数 (2)

承接上文:JavaWeb课程复习资料(七)——select服务查询所有功能编写

1、添加【AddServlet】

2、添加AddServlet编码

代码语言:javascript复制
package com.item.servlet;

import com.item.dao.UserInfoDAO;
import com.item.daoimpl.UserInfoDAOImpl;
import com.item.pojo.UserInfo;

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 java.io.IOException;
import java.util.List;

@WebServlet("/SelectAllServlet")
public class SelectAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserInfoDAO db = new UserInfoDAOImpl();
        List<UserInfo> list = db.GetAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

3、在【index.jsp】中添加【添加】按钮编码

代码语言:javascript复制
 <a href="Add.jsp" class="btn btn-primary">添加</a>

4、创建【AddServlet.jsp】

5、添加【AddServlet.jsp】编码

代码语言:javascript复制
<%--
  Created by IntelliJ IDEA.
  User: qwe84
  Date: 2022/2/26
  Time: 11:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
    <form action="/AddServlet" method="post">
        <p>
            <input type="text" placeholder="请输入用户名" name="userName" class="form-control" required/>
        </p>
        <p>
            <input type="text" placeholder="请输入爱好" name="introduce" class="form-control" required/>
        </p>
        <p>
            <input type="submit" value="添加" class="btn btn-primary"/>
        </p>
    </form>
</body>
</html>

6、执行测试

点击【左上角的添加功能】

添加成功,可以看到【sunwukong】的值是【七十二变】。

0 人点赞