Java教程

JavaWeb课程复习资料(七)——select服务查询所有功能编写

本文主要是介绍JavaWeb课程复习资料(七)——select服务查询所有功能编写,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

承接上文:JavaWeb课程复习资料(六)——DAO与DAOImpl层封装

目录

1、查询所有数据

2、编写index.jsp页面

3、访问测试


1、查询所有数据

源码

package com.item.servlet;

import com.item.dao.UserInfoDAO;
import com.item.daoimpl.UserInfoDAOImpl;
import com.item.pojo.UserInfo;

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("/SelectAllServlet")
public class SelectAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserInfoDAO db = new UserInfoDAOImpl();
        List<UserInfo> list = db.GetAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

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

2、编写index.jsp页面

<%@ page import="com.item.pojo.UserInfo" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 红目香薰
  Date: 2022/2/25
  Time: 17:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  </head>
  <body>
  <% List<UserInfo> list=(List<UserInfo>)request.getAttribute("list");%>
  <table class="table table-hover table-bordered">
    <tr>
      <th>编号</th>
      <th>创建时间</th>
      <th>用户名</th>
      <th>简介</th>
      <th>操作</th>
    </tr>
    <%
      for (UserInfo info:list ) {
    %>
        <tr>
          <td><%=info.getId()%></td>
          <td><%=info.getCreateDate()%></td>
          <td><%=info.getUserName()%></td>
          <td><%=info.getIntroduce()%></td>
          <td>
              <a href="UpdateByIdServlet?id=<%=info.getId()%>" class="btn btn-primary">修改</a>
              <a href="DeleteByIdServlet?id=<%=info.getId()%>" class="btn btn-primary">删除</a>
          </td>
        </tr>
    <%
      }
    %>
  </table>
  </body>
</html>

3、访问测试

启动后直接进入主页

 访问成功。

注:如果出现【No suitable driver found for jdbc】类似的报错,找到自己的【mysql-connector-java-5.1.39.jar】包,放到对应的识别路径上就好。

这篇关于JavaWeb课程复习资料(七)——select服务查询所有功能编写的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!