简单的servlet+jsp综合案例

综合案例(无数据库)

servlet

@WebServlet("/userManage")
public class UserManage extends HttpServlet {

    private Map<String, User> userMap = new HashMap<String,User>();

    public UserManage(){
        User user1 = new User("1","小明",20);
        User user2 = new User("2","小红",30);
        User user3 = new User("3","小刚",18);
        User user4 = new User("4","小牛",27);
        userMap.put("1",user1);
        userMap.put("2",user2);
        userMap.put("3",user3);
        userMap.put("4",user4);
    }

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

        String type = req.getParameter("type");

        if(StringUtils.isEmpty(type)){
            type = "findAll";
        }

        switch (type){
            case "delete":
                String id = req.getParameter("id");
                userMap.remove(id);
                resp.sendRedirect("/userManage");
                break;
            case "findAll":
                req.setAttribute("userList",userMap.values());
                req.getRequestDispatcher("userList.jsp").forward(req,resp);
                break;
            case "update":
                id = req.getParameter("id");
                User user = userMap.get(id);
                req.setAttribute("user",user);
                req.getRequestDispatcher("/userUpdate.jsp").forward(req,resp);
                break;
        }
    }

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

        req.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");
        String name =  req.getParameter("name");
        String age =  req.getParameter("age");

        User user = new User(id,name,Integer.valueOf(age));
        userMap.put(id,user);

        resp.sendRedirect("/userManage");
    }
}

列表页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>用户列表</title>
</head>
<body>
    <a href="/userAdd.jsp">新增</a>
    <table border="1px">
        <tr>
            <th>操作</th>
            <th>用户号</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <c:forEach items="${userList}" var="user">
            <tr>
                <td>
                    <a href="/userManage?type=update&id=${user.id}">编辑</a>
                    <a href="/userManage?type=delete&id=${user.id}">删除</a>
                </td>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.age}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

新增页面&编辑页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户新增</title>
</head>
<body>
    <form action="/userManage" method="post">
        用户名:<input name="id" id="id" type="text" /><br/>
        姓名:<input name="name" id="name" type="text" /><br/>
        年龄:<input name="age" id="age" type="text" /><br/>
        <input type="submit" name="保存" value="保存" />
    </form>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户编辑</title>
</head>
<body>
    <form action="/userManage" method="post">
        用户名:<input name="id" id="id" type="text" value="${user.id}" readonly/><br/>
        姓名:<input name="name" id="name" type="text" value="${user.name}"/><br/>
        年龄:<input name="age" id="age" type="text" value="${user.age}"/><br/>
        <input type="submit" name="保存" value="保存" />
    </form>
</body>
</html>
上一篇:snprintf()解析


下一篇:Java8、9中的Collection API的增强功能