【问题标题】:Java servlet stops printing table half way throughJava servlet 中途停止打印表格
【发布时间】:2017-08-31 00:39:04
【问题描述】:

我在尝试使用我的 servlet 打印 HTML 表格时遇到了一个令人困惑的错误。最后一行打印 4/8 个元素,然后停止。前 7 行没有问题,与第 8 行基本一致。

这是我的 Java 代码:

...
out.println("<tr>");
j = 0;
for (int i=56; i<64; i++){
    j++;
    Integer.toString(j);
    out.println("<td ");
    if (seats[i].label.equals(label)){
        seats[i].booked = true;
        seats[i].id = id;
        seats[i].phone = phone;
        seats[i].address = address;
        seats[i].email = email;
    }
    if (!seats[i].booked){
        out.println("bgcolor='#7CFC00'");
    }
    out.println("><a");
    if (!seats[i].booked){
        out.println(" href='Booking?label=H"+j+"'");
    }
    out.println(">H"+j+"</a></td>");
}
out.println("</trr");

这是我的浏览器生成的 HTML 代码:

...
<tr>
<td bgcolor="#7CFC00"><a 
href="http://localhost:8080/assignment1/Booking?
label=H1">H1</a></td>
<td bgcolor="#7CFC00"><a 
href="http://localhost:8080/assignment1/Booking?
label=H2">H2</a></td>
<td bgcolor="#7CFC00"><a 
href="http://localhost:8080/assignment1/Booking?
label=H3">H3</a></td>
<td bgcolor="#7CFC00"><a 
href="http://localhost:8080/assignment1/Booking?
label=H4">H4</a></td>
</tr></tbody></table>

座位的数组长度为 100,因此即使这就是元素不显示的原因,也有足够的空间。该循环应该执行 8 次,并显示剩余的四个必需元素,就像前 7 个循环成功执行的那样。希望得到一些帮助,因为我真的很难过,需要在为时已晚之前完成我的第一个 servlet 任务。

【问题讨论】:

    标签: java html servlets html-table


    【解决方案1】:

    我已在我的本地系统中的 Main.java 的以下代码(因为我没有 seat.ser)评论:

    //Deseriailzing seating
                /*try {
                    FileInputStream fileIn = new FileInputStream("seating.ser");
                    ObjectInputStream in = new ObjectInputStream(fileIn);
                    seats = (Seat[]) in.readObject();
                    in.close();
                    fileIn.close();
                } catch (IOException i) {
                    i.printStackTrace();
                    out.println("IOException i");
                    //return;
                } catch (ClassNotFoundException c) {
                    out.println("Employee class not found");
                    c.printStackTrace();
                    //return;
                }*/
    

    //  ObjectOutputStream oos = null;
                //          FileOutputStream fout = null;
    
                /*try {
                  FileOutputStream fileOut = new FileOutputStream("seating.ser");
                  ObjectOutputStream out2 = new ObjectOutputStream(fileOut);
                  out2.writeObject(seats);
                  out2.close();
                  fileOut.close();
                  System.out.printf("Serialized data is saved in seating.ser");
                  }catch(IOException i) {
                  i.printStackTrace();
                  }*/
    

    然后执行。它工作正常。所以问题出在您的 seat.ser 文件或上面的注释代码中。

    这是我的 Seat 类(在 Main.java 中使用)代码:

    public class Seat {
        String label,phone,address,email,bookingTime;
        String id="";
        boolean booked;
    }
    

    我在这里附上输出屏幕截图。请看。

    【讨论】:

      【解决方案2】:

      我认为问题出在out.println("&lt;/trr");。请尝试使用out.println("&lt;/tr");

      【讨论】:

      • 感谢您的回复。我已经尝试过了,很遗憾,它没有任何区别。
      • 需要查看整个代码,例如什么是“标签”值以及 和 标记的起始位置。你得到什么错误/异常??
      • 是的,好点。如果您愿意看一下,我有一个可共享的 Google Drive 文件夹,其中包含我项目的所有文件。引用的代码在 Main.java 中。我的代码不必要地冗长且非结构化,因为我一直很忙并且仍在学习。我不知道错误/异常是什么,因为我只编译了代码并使用 Tomcat 在浏览器中打开了它。我不确定如何查看 servlet 的异常。
      • 未正确打印的部分是文件末尾的H行,已被注释掉。
      • 我真的应该养成边走边评论的习惯,所以提前道歉。
      猜你喜欢
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2019-11-30
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多