Java servlet在中途停止打印表

时间:2017-08-18 15:43:34

标签: java html servlets html-table

尝试使用我的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任务。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我在本地系统中评论了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;
}

我附在这里输出屏幕截图。请看。 enter image description here