欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Servlet+JSP实现简单的用户管理(无数据库)增删改查

程序员文章站 2022-03-26 17:57:36
项目结构:用户实体类User,后台管理类(servlet类)UserManage.java用户列表页userList.jsp,用户新增页userAdd.jsp,用户修改页userUpdate.jsp实现功能:展示用户列表,用户新增、修改、删除功能效果图如下:userList.jsp<%@ page import="java.util.List" %><%@ page import="POJO.User" %><%@ page import="java.uti...

项目结构:用户实体类User,
后台管理类(servlet类)UserManage.java
用户列表页userList.jsp,用户新增页userAdd.jsp,
用户修改页userUpdate.jsp
实现功能:展示用户列表,用户新增、修改、删除功能
效果图如下:
Servlet+JSP实现简单的用户管理(无数据库)增删改查
Servlet+JSP实现简单的用户管理(无数据库)增删改查userList.jsp

<%@ page import="java.util.List" %>
<%@ page import="POJO.User" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table border="1" cellspacing="0" align="center" bgcolor="#f0ffff">
    <tr><td><a href="userAdd.jsp">新增</a></td></tr>
    <tr style="font-weight: bold">
        <td colspan="2" style="text-align: center;">操作</td>
        <td>id</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    <c:forEach items="${list}" var="user">
        <tr>
            <td><a href="UserManage?method=findById&id=${user.id}">编辑</a></td>
            <td><a href="UserManage?method=delete&id=${user.id}">删除</a></td>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

userAdd.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="UserManage" method="post">
    <table cellspacing="0" border="1 solid black" bgcolor="#f0ffff" align="center">
        <tr>
            <td>id:</td>
            <td><input type="text" name="id"></td><br/>
        </tr>
        <tr>
            <td>名字:</td>
            <td><input type="text" name="name"></td><br/>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" name="age"></td><br/>
        </tr>
        <tr>
            <td>
            <input type="submit" value="保存" align="">
            <input type="reset" value="重置" align="">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

userUpdate.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="UserManage" method="post">
    <table cellspacing="0" border="1 solid black" bgcolor="#f0ffff" align="center">
        <tr>
            <td>id:</td>
        <td>
            <input type="text" name="id" value="${user.id}" readonly></td>
        </tr>
        <br/>
        <tr>
            <td>名字:</td>
        <td>
            <input type="text" name="name" value="${user.name}"></td>
        </tr>
        <br/>
        <tr>
            <td>年龄:</td>
        <td>
            <input type="text" name="age" value="${user.age}"></td>
        </tr>
        <br/>
        <td><input type="submit" value="保存"></td>
    </table>
</form>
</body>
</html>

UserManage.java

import POJO.User;

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.HashMap;
import java.util.Map;

@WebServlet("/UserManage")
public class UserManage extends HttpServlet {
    private Map<Integer, User> map = new HashMap<>();
    
    public UserManage() {
        map.put(1, new User("1", "刘备",  "18"));
        map.put(2, new User("2", "孙权", "20"));
        map.put(3, new User("3", "曹操", "30"));
        map.put(4, new User("4", "项羽", "21"));
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null) {
            method = "save";
        }
        String idStr = req.getParameter("id");
        String name = req.getParameter("name");
        String scoreStr = req.getParameter("age");
        Integer id = Integer.parseInt(idStr);
        Integer score = Integer.parseInt(scoreStr);
        User user = new User(idStr, name, scoreStr);
        map.put(id, user);
        resp.sendRedirect("/web/UserManage");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findall";
        }
        if (method.equals("delete")) {
            String idStr = req.getParameter("id");
            Integer id = Integer.parseInt(idStr);
            map.remove(id);
            resp.sendRedirect("/web/UserManage");
        } else if (method.equals("findall")) {
            req.setAttribute("list", map.values());
            req.getRequestDispatcher("userList.jsp").forward(req, resp);
        } else if (method.equals("findById")) {
            String idStr = req.getParameter("id");
            Integer id = Integer.parseInt(idStr);
            User use = map.get(id);
            req.setAttribute("user", use);
            req.getRequestDispatcher("userUpdate.jsp").forward(req, resp);
        }
    }
}

User.java

package POJO;

public class User {
    private String id;
    private String name;
    private String age;
    public User(String id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

Servlet+JSP实现简单的用户管理(无数据库)增删改查

本文地址:https://blog.csdn.net/weixin_46009468/article/details/112229982