如何使用servlet使用数组列表中的数据填充html?

问题描述 投票:0回答:1

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

**This is where i'm loading my data and populating it inside my arraylist**
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);*/
    }

}

**My html table**
<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个回答
0
投票

实际上,这非常简单-假设您熟悉将对象传递到JSP的过程。这是一个使用您传递并命名为message的对象的示例,我仅假设它具有一个名为Stringname属性。

<%for (StudentClass sc: message) {%>
    <span><%=sc.name%></span><br>
<%}%>
© www.soinside.com 2019 - 2024. All rights reserved.