目录结构
执行流程图
执行结果(修改和删除功能未实现)
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 16949
Date: 2022/5/7
Time: 15:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%--<c:out value="${pageContext.request.contextPath}"></c:out>--%>
<a href="${pageContext.request.contextPath}/FindAllUserServlet">查询所有用户信息</a>
</body>
</html>
userlist.jsp
<%--
Created by IntelliJ IDEA.
User: 16949
Date: 2022/5/7
Time: 15:51
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<table border="1px" width="600px" bgcolor="#ffb6c1" align="center">
<tr>
<th>用户序号</th>
<th>用户id </th>
<th>用户姓名</th>
<th>用户性别</th>
<th>用户年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" var="user" varStatus="vs">
<tr>
<th>${vs.count}</th>
<th>${user.uid}</th>
<th>${user.username}</th>
<th>${user.sex}</th>
<th>${user.age}</th>
<th>
<a href="#">修改</a>
<a href="#">删除</a>
</th>
</tr>
</c:forEach>
</table>
</body>
</html>
User类
package com.itheima.domain;
import java.io.Serializable;
public class User implements Serializable {
private String uid;
private String username;
private String sex;
private String age;
public User() {
}
public User(String uid, String username, String sex, String age) {
this.uid = uid;
this.username = username;
this.sex = sex;
this.age = age;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
FindAllUserServlet类
package com.itheima.web;
import com.itheima.domain.User;
import com.itheima.service.UserService;
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(urlPatterns = "/FindAllUserServlet")
public class FindAllUserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=utf-8");
UserService service = new UserService();
List<User> users = service.findAllUsers();
request.setAttribute("users",users);
request.getRequestDispatcher("/userList.jsp").forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
}
UserService类
package com.itheima.service;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import java.util.List;
public class UserService {
public List<User> findAllUsers() {
UserDao dao = new UserDao();
List<User> users = dao.findAllUsers();
return users;
}
}
UserDao类
package com.itheima.dao;
import com.itheima.domain.User;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> findAllUsers() {
List<User> users = new ArrayList<>();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/user.txt"),"UTF-8"));
String line;
while((line=bf.readLine())!=null){
User user = new User();
user.setUid(line.split("\\,")[0]);
user.setUsername(line.split("\\,")[1]);
user.setAge(line.split("\\,")[3]);
user.setSex(line.split("\\,")[2]);
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
}
|