-1

Here is my code:

  try {

    String name = "";
    String id1 = empid.getEmpId(id);
    System.out.println("id is ===> " + id1);
    Map < Object, Object > map = reporteeservice.getReportees(id1);
    Set < Map.Entry < Object, Object >> s1 = map.entrySet();
    PrintWriter out1 = response.getWriter();
    out1.println("<html><head><style>\r\n" + "table {\r\n" + "  font-family: arial, sans-serif;\r\n" + "  border-collapse: collapse;\r\n" + "  width: 100%;\r\n" + "}\r\n" + "\r\n" + "td, th {\r\n" + "  border: 1px solid #dddddd;\r\n" + "  text-align: left;\r\n" + "  padding: 4px;\r\n" + "}\r\n" + "\r\n" + "</style>\r\n" +
     "</head><center><font size=\"20\"><body><h2>Reportees List</h2></font>" + "<table>\r\n" + "<tr>\r\n" + "<th>Number</th>" + "<th>User Id</th>" + "<th>Username</th>\r\n</center>" + "<body>");


    for (Iterator < Map.Entry < Object, Object >> iterator = s1.iterator(); iterator.hasNext();) {
     Map.Entry < Object, Object > entry = iterator.next();
     Object name1 = entry.getKey();
     Object value = entry.getValue();
     int num = Number++;

     String values = value.toString();
     //System.out.println("returning map");
     PrintWriter out = response.getWriter();
     System.out.println("values ==> " + values);
     request.setAttribute("empid", values);
     out1.println(

      // "<html><body><table>\r\n" +
      // "<tr>\r\n" +
      // "<th>User Id</th>\r\n" +
      // "<th>Username</th>\r\n" +
      "<table>" + "</tr>\r\n" + "<tr>\r\n" + "<td>" + num + "</td>" + "<td>" + values.toUpperCase() + "</td>" + "<td><a href='./Response1?empidVal=" + values + "'>" + name1 + "</a></td>" + "</tr>\r\n" + "</table></body></html>");

    }

Actually I am trying to add the map objects which are dynamic(as they are being iterated over the loop)to html table in an java servlet. My output is:

  Number                           User Id                     Username
   1              AR12355                                Anagha 
   2            MS12345                         Madhusu S
   3             AT12345                          Amreen Tai

But I want the output in an aligned manner. But I am not getting an idea how to append the values to the table.Please help.Thanks in advance

KishanCS
  • 1,357
  • 1
  • 19
  • 38
RAMYA
  • 89
  • 1
  • 8
  • Hint: Take a look at what tag you open and what you close. Order of these statements is important. – BackSlash Jan 15 '19 at 11:49
  • the line "\r\n" under is wrong. At this moment i gues no tag is open, because you just startet a new table with
    – YingYang Jan 15 '19 at 12:02

2 Answers2

0

Your opening/closing <table>/</table> are not symetric.

In addition, you should never have </body> and </html> tags in your iteration, because they should be uniq in the result.

Anyway, it will be easier for you to produce an output, and to validate it with w3c validator, and improve your model till you get a validated one.

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
0

A distinct non-answer: don't generate HMTL manually like this. People did that 20 years ago, but it is really a bad idea in 2019.

Why? Because you can easily generate invalid HTML, as your code just does, as the comments imply that you are at one point generating a closing TR tag, without having a matching opening one before.

So, the real answer is: use an existing tool, such as JSoup. See here for some more ideas.

GhostCat
  • 137,827
  • 25
  • 176
  • 248