查询用户功能实现
QuerryAllServlet
package Servlet; import Bean.User; import Dao.UserDao; 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.io.PrintWriter; import java.util.List; @WebServlet("/QueryAllServlet") public class QueryAllServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // -----分割线----- List<User> list=new UserDao().getall(); request.setAttribute("list",list); request.getRequestDispatcher("/queryall.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
queryall.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/4/22 Time: 10:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <script language="JavaScript"> function p_del() { var msg = "您真的确定要删除吗?\n\n请确认!"; if(confirm(msg)==true){ return true; }else { return false; } } </script> <body> <form action="/QueryAllServlet" method="post"> <input type="submit" value="查询所有数据"/> </form> <table border="1" width="600px" align="center"> <tr> <td>用户名</td> <td>邮箱</td> <td>生日</td> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.username}</td> <td>${user.email}</td> <td>${user.birthday}</td> <td><a href="/DeleteServlet?uid=${user.id}" onclick="javascript:return p_del()">删除</a> <a href="/UpdateQueryServlet?uid=${user.id}">修改</a> </td> </tr> </c:forEach> </table> <%--测试项目:按ID去查询前三个--%> <%--<form action="/QueryAllServlet" method="post">--%> <%--<input type="submit" value="查询前三数据"/>--%> <%--</form>--%> <%--<table border="1" width="600px" align="center">--%> <%--<tr>--%> <%--<td>用户名</td>--%> <%--<td>邮箱</td>--%> <%--<td>生日</td>--%> <%--</tr>--%> <%--<c:if test="${list.id>3}" var="pd">--%> <%--<tr>--%> <%--<td>${pd.username}</td>--%> <%--<td>${pd.email}</td>--%> <%--<td>${pd.birthday}</td>--%> <%--</tr>--%> <%--</c:if>--%> </table> </body> </html>