试图在Netbeans中打印出一个html表

时间:2016-04-15 19:43:48

标签: java html mysql servlets netbeans

我正在学习使用java和netbeans与数据库交谈。 我有一个名为tunafishes的数据库,其中包含几个信息位。我想使用netbeans打印一个html表。我以为我会把所有东西都搞定,但是当我试图打印桌子时,没有任何东西出来。不知道我错过了什么,或者我在错误的文件中......

TunaServlet的代码

package ViewLayer;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import businesslayer.TunasBusinessLogic;
import transferobjects.Tuna;
import java.util.List;

/**
*
* @author Samuel
*/
public class TunaServlet extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sam Murdock</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Sam Murdock</h1>");
        out.println("date:" + java.time.LocalDate.now().toString());
        out.println("<br>");
        out.println("time:"+java.time.LocalTime.now().toString());
        out.println("<br>");
        TunasBusinessLogic logic = new TunasBusinessLogic();
        List<Tuna>tunas=logic.getAllTunas();
        out.println("<table>");
        out.println("<tr>");
            out.println("<td>Tuna Id</td>");
            out.println("<td>Genus Name</td> ");
            out.println("<td>Species Name</td>");
            out.println("<td>Common Name</td>");
            out.println("<td>Max Weight</td>");
            out.println("<td>IUCN Status</td>");
        out.println("</tr>");
        for(Tuna tuna: tunas){
            out.printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%f</td><td>%s</td></tr>",
                    tuna.getTunaId(), tuna.getGenusName(),tuna.getSpeciesName(), tuna.getCommonName(), tuna.getMaxWeight(), tuna.getIucnStatus());
            out.println("</table>");
        }out.println("</body>");
        out.println("</html>");
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

如果我错过了一个文件,你需要让我知道,我也会复制它。它打印我的名字,时间和日期没有问题,但然后不打印任何表格。

1 个答案:

答案 0 :(得分:0)

你正在关闭for循环中的表,将它移到外面循环

.... 
List<Tuna>tunas=logic.getAllTunas();
out.println("<table>");
out.println("<tr>");
  out.println("<td>Tuna Id</td>");
  out.println("<td>Genus Name</td> ");
  out.println("<td>Species Name</td>");
  out.println("<td>Common Name</td>");
  out.println("<td>Max Weight</td>");
  out.println("<td>IUCN Status</td>");
out.println("</tr>");
for(Tuna tuna: tunas){
     out.printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%f</td><td>%s</td></tr>",tuna.getTunaId(), tuna.getGenusName(),tuna.getSpeciesName(), tuna.getCommonName(), tuna.getMaxWeight(), tuna.getIucnStatus());
    }
    //* move table closing tag out of for loop
    out.println("</table>");
    out.println("</body>");
    out.println("</html>");
}