0

I am trying to populate an html data with data from my arraylist, but i am having some difficulties. I already loaded the data and stored it inside my arraylist but when launching my program the table is empty. Could someone help me with this? I already tried googling my problem, but i cant seem to find an answer.

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>
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
Han Seria
  • 15
  • 3

2 Answers2

2

Although @olivakyle's answer is correct, I prefer the use of jstl tags instead of scriplets: it keeps the JSP less messy:

<!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>
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

It's quite simple actually – given that you're familiar in passing objects to your JSP. Here is an example using the object you passed and named message and I'm just assuming it has a String property named name.

<%for (StudentClass sc: message) {%>
    <span><%=sc.name%></span><br>
<%}%>
kecoliva
  • 80
  • 1
  • 11