如何使用jsp输出用户列表?

时间:2018-12-19 20:57:15

标签: java jsp servlets

我有一个类Person,其参数:名称,年龄,电子邮件以及getter和setter。之后,我创建了PersonStorage类:

    private static List<Person> personList = new ArrayList<Person>();

public void addPerson(Person person){
    personList.add(person);
}

public List<Person> getAllPerson(){
    return this.personList;
}

我的HttpServlet-getParameter:名称,年龄,电子邮件和创建对象Person并添加到列表中:

    @WebServlet(urlPatterns = {"/addclientform.jsp"})
public class AddClientServlet extends HttpServlet {

    private PersonStorage personList = new PersonStorage();

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

        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String email = req.getParameter("email");

        Person person = new Person(name, age, email);
        personList.addPerson(person);

        req.getRequestDispatcher("printmessage.jsp").forward(req,resp);
    }
}

在我的jsp表格下面:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add Client Form</title>
</head>
<body>

<font size="5">Add Client Form</font><br/>
<br/>
<form action="printmessage.jsp" method="post">

    Name: <input type="text" name="name"/><br/>
    Age: <input type="text" size="1" name="age"/><br/>
    Email: <input type="text" name="email"/><br/>
    <br/>
    <input type="submit" value="Add Client"/>

</form>

<form name="return" action="home" method='post'>
    <input type='submit' value='Return Home'/>
</form>

</body>
</html>

我还创建了一个HttpServlet巫婆输出用户列表:

    @WebServlet(urlPatterns = {"/clientList"})
public class SeeClient extends HttpServlet {

    PersonStorage personStorage = new PersonStorage();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("list", personStorage.getAllPerson());
        req.getRequestDispatcher("clientList.jsp").forward(req,resp);
    }
}

最后一个带有forEach的jsp文件:

    <%@ page session="false" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>Clien Page</title>
</head>
<body>

<table>
    <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Email</th>
    </tr>

    <c:forEach items="${list}" var="personlist">
        <tr>
            <td>${personlist.name}</td>
            <td>${personlist.age}</td>
            <td>${personlist.email}</td>
        </tr>
    </c:forEach>

</table>

<form name="home" action="home.jsp" method="post">
    <input type="submit" value="back">
</form>

</body>
</html>

我的printmessage jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<html>
<head>
    <title>Message</title>
</head>
<body>
<font size="5">Message...</font><br/>
<br/>
<font size="4">Operation completed successfully! </font><br/>
<br/>

<form name="return" action="home.jsp" method='post'>
    <input type='submit' value='Return Menu'/>
</form>

</body>
</html>

但是当我想添加新用户并显示它们时,我什么也看不到。 我该如何解决?有人可以帮我吗?

0 个答案:

没有答案