简体   繁体   中英

Java servlet stops printing table half way through

I am encountering a confusing bug when attempting to print a HTML table with my servlet. The last row is printing 4/8 elements and then stopping. There is no problem with the first 7 rows, which are practically identical to the 8th.

Here is my Java code:

...
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");

And here is the resulting HTML code from my browser:

...
<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>

The array length of seats is 100 so there is plenty of space even if that were why the elements are not displaying. The loop should execute 8 times and show the remaining four required elements as the previous 7 loops did successfully. Would love some help as I'm honestly stumped and need to get my first servlet assignment done before it's too late.

I think problem is at out.println("</trr"); . Please try to use out.println("</tr");

I have commented below code(because I don't have seating.ser) of your Main.java in my local system:

//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;
            }*/

and

//  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();
              }*/

then executed. It's working fine.So the Issue is in your seating.ser file or above commented code.

Here is my Seat class(this used in Main.java) code:

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

I'm attaching here output screen-shot.Please see. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM