showall.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>所有用户页面</title>
</head>
<body>
<h1>${xiaoxi}</h1>
<table width="600" border="1" cellpadding="0" >
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>密码</th>
<th>家乡</th>
<th>备注</th>
<th>操作</th>
</tr>
<c:forEach var="U" items="${userAll}" >
<form action="UpdateServlet" method="post">
<tr>
<td><input type="text" value="${U.id}" name="id" ></td>
<td><input type="text" value="${U.name}" name="name"></td>
<td><input type="text" value="${U.sex}" name="sex"></td>
<td><input type="text" value="${U.pwd}" name="pwd"></td>
<td><input type="text" value="${U.home}" name="home"></td>
<td><input type="text" value="${U.info}" name="info"></td>
<td><a href="DeleteServlet?id=${U.id}">删除</a> <input type="submit" value="更新"/></td>
</tr>
</form>
</c:forEach>
</table>
</body>
</html>
SearchAllServlet.java程序如下,导包之类的略
public class SearchAllServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
UserDao ud = new UserDaoImpl();
List<User> userAll = ud.getUserAll();
request.setAttribute("userAll", userAll);
request.getRequestDispatcher("/showall.jsp").forward(request, response);
}
}
数据库查询到的所有用户数据保存在List集合userAll中,通过request.setAttribute("userAll",userAll)传入jsp
jsp通过<c:forEach var="U" items="${userAll}" >取出数据并进行遍历
以上,需要注意的是showall中用到了<c:forEach>标签,并且传值的时候使用了el表达式${...},因此还需要导入包jstl.jar和standard.jar并且在showall.jsp中增加:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
ps:所写是在原博主的内容之后的补充
转载原文及源代码链接:https://blog.csdn.net/qq_23994787/article/details/73612870