【问题标题】:How do you populate an html with data from an array list using servlet? [duplicate]如何使用 servlet 使用数组列表中的数据填充 html? [复制]
【发布时间】:2020-04-09 08:10:49
【问题描述】:

我正在尝试使用我的数组列表中的数据填充 html 数据,但我遇到了一些困难。我已经加载了数据并将其存储在我的数组列表中,但是在启动我的程序时,表是空的。有人可以帮我解决这个问题吗?我已经尝试过用谷歌搜索我的问题,但我似乎找不到答案。

这是我加载数据并将其填充到我的数组列表中的地方

import java.io.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet("/registered-class")
public class myRegist extends HttpServlet{

    @Override
    public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException {

        getInfo registerStu = new findInfo();

        String ssn = request.getParameter("ssn");
        String fullName = registerStu.getFullName(ssn);
        String phone = registerStu.getPhone(ssn);

        List<StudentClass> classList = registerStu.loadClass(ssn);


        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String docType =
                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
        "Transitional//EN\">\n";

        StudentClass stu = new StudentClass();

        request.setAttribute("message", classList);
         request.getRequestDispatcher("/table.jsp").forward(request,response);


        //ptints out info about course
         //out.println(classList.get(0));

    /*StudentInfo info = new StudentInfo(ssn, fullName, phone);

        HttpSession session = request.getSession();
        session.setAttribute("student", info);

        String address = null;
         if (fullName == null) {
              address = "/myRegist.jsp";
            } 
         else if (fullName != null) {
              address = "/myRegist.jsp";
            }
         RequestDispatcher dispatcher =
                  request.getRequestDispatcher(address);
                dispatcher.forward(request, response);*/
    }

}

我的 html 表格

<html>
    <body>
    <head>
    <title>
    View Books
    </title>
    </head>
    <body>
    <table border=2>
    <tr>
        <th>Book ID</th>
        <th>Title</th>
        <th>Author</th>
        <th>No. of copies  AVAILABLE</th>
        <th>Number of favourites</th>
    </tr>


    <tr>
        <td><%=b.bookID%></td>
        <td><%=b.bookTitle%></td>
        <td><%=b.bookAuthor%></td>
        <td><%=b.bookCopies%></td>
        <td><%=b.bookFavs%></td>    
    </tr>
    <%
        }
    %>
    </table>

    </body>
</html>

【问题讨论】:

    标签: java mysql servlets


    【解决方案1】:

    虽然@olivakyle 的回答是正确的,但我更喜欢使用jstl 标签而不是scriplets:它使JSP 不那么混乱:

    <!DOCTYPE html>
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
        <body>
        <head>
        <title>
        View Books
        </title>
        </head>
        <body>
        <table border=2>
        <tr>
            <th>Student ID</th>
            <th>Name</th>
        </tr>
    
        <c:forEach var="stu" items="${message}">
        <tr>
            <td>${stu.id}</td>
            <td>${stu.name}</td>
        </tr>
        </c:forEach>
        </table>
    
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      实际上这很简单——假设您熟悉将对象传递给您的 JSP。这是一个使用您传递并命名为 message 的对象的示例,我只是假设它有一个名为 nameString 属性。

      <%for (StudentClass sc: message) {%>
          <span><%=sc.name%></span><br>
      <%}%>
      

      【讨论】:

      • Scriptlets 自 2003 年起正式禁止使用。完全不向初学者推荐它们。另见stackoverflow.com/q/3177733
      • 注意 - 感谢您与我分享这些知识。 :)
      猜你喜欢
      • 2014-07-19
      • 2021-09-02
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2021-09-03
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多