On January 27th, this site will be read-only as we migrate to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,626 Users
  • 2,269,768 Discussions
  • 7,916,798 Comments

Discussions

How do you write an output in columns to an html table?

2801625
2801625 Member Posts: 57
edited Dec 3, 2014 8:06AM in New To Java

Ive written two programs. My first program compares the difference of two text files and prints the differences in an output. My second program creates a simple Html table. I would like to write my output to this table. How would I go about doing this in java?

Thanks in advance for your help.

Tagged:

Answers

  • Aman Sachdeva
    Aman Sachdeva Analyst HyderabadMember Posts: 7

    You have to use Servlet and JSP in java to achieve this thing.

    In Servlet file, calculate the difference in two files just like you are doing in one of the programs.

    In JSP file, you'll create an HTML table and then populate the data in the table

    If you are very new to java and do not anything about Servlet and JSP, then you have to learn how they work in order to proceed further.

    You can use these links to learn Servlets and JSP. 

    Official Documentation Link: JSP, If you want to skip the complete documentation this is other Link: Example

    Official Documentation Link: Servlets  If you want to skip the complete documentation this is other Link: Example

    Other useful link: Follow me

  • TPD-Opitz
    TPD-Opitz Dipl.-Ing, Dipl.-Inf GermanyMember Posts: 2,465 Silver Trophy

    since you did not provide any example code it's hard to give any advice.

    But, you state that your program already creates a HTML table.

    How does it do this?

    Is it simply outputting a single string with that simple HTML content?

    The simplest approach is to split up that string an add more HTML content in a loop as needed:

    StringBuilder htmlTable = new StringBuilder("<html><table>\n");
    htmlTable.append("<th><td>number</td><td>double</td><td>square</td></th>\n");
    for(int i=0;i<10;i++) {
    htmlTable.append("<tr>");
    
    htmlTable.append("<td>");
    htmlTable.append(i);
    htmlTable.append("</td>");
    
    htmlTable.append("<td>");
    htmlTable.append(i*2);
    htmlTable.append("</td>");
    
    htmlTable.append("<td>");
    htmlTable.append(i*i);
    htmlTable.append("</td>");
    
    htmlTable.append("</tr>\n");
    }
    htmlTable.append("</table></html>");
    System.out.println(htmlTable.toString());
    

    Of cause there are smarter ways to do this, eg. using a template framework, but I guess this is appropriate to your skill level.

    bye

    TPD

  • 2801625
    2801625 Member Posts: 57
    edited Dec 2, 2014 11:55AM

    Thanks guys for the insight. Im very new to java so I'm a complete novice when it comes to writing code. What I came to realize was I needed to do was build a dynamic table. My issue was that I was building a static table and I couldint populate the table with my results. Below is an example of the code I wrote for my table. My apologies if didnt format my code correctly.

    import java.io.BufferedWriter;

    import java.io.File;

    import java.io.FileWriter;

    import java.io.IOException;

    import java.util.Set;

    public class HtmlDataTable {

      public void writehtmltable(Set<String> filenameSet){

      //creates table in the following path

      File htmltable = new File("C:/Output.html");

      try {

      BufferedWriter bw = new BufferedWriter(new FileWriter(htmltable));

      //write html data table

      bw.write(htmltop);

      for (String jarfilename:filenameSet){

      String line="<tr>  <td> </td>      <td> </td> </td>      <td>"+jarfilename + " </tr>";

      bw.write(line);

      }

     

      bw.write(htmlbottom);

      //close the resource

      bw.close();

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

    private static String htmltop = "<!DOCTYPE html>\n"+

                                      "<html>\n"+

                                         "<head>"+

                                         "<title>Jar Filename Existance Check</title>"+

                                         "<style>"+

                                         "table, th, td {"+

                                         "    border: 2px solid black;"+

                                         "}"+

                                         "</style>"+

                                         "</head>"+

                                         "<body>"+

                                         "<table>\n"+

                                         "   <tr>     <th colspan='100'>" +

                                         "<h3><br>Jarfilename Existance Check</h3>      </th>   </tr>" +

                                         "<th>POM File Data</th> <th>Lib Directory Files</th> <th>Missing Jarfiles</th>";

      private static String htmlbottom = "</table></body></html>";

    public static void main(String[] args) {

      new HtmlDataTable().writehtmltable(null);

  • TPD-Opitz
    TPD-Opitz Dipl.-Ing, Dipl.-Inf GermanyMember Posts: 2,465 Silver Trophy

    what is it in your words that makes a HTML table dynamic instead of static?

    Are you familiar  with HTML or is this also something you are new to?

    Do you know that, and if so why your java code outputs nothing?

    bye

    TPD

  • 2801625
    2801625 Member Posts: 57
    edited Dec 2, 2014 12:03PM

    Sorry I failed to include the entire code from my program. I have since edited my previous post.

    I needed my table to have the ability to change based on the output I was trying to write to it.

    The table I was able to create previously was fixed and my output wasnt being written to it.

    This is the difference between the table being dynamic vs. static.

    I truly appreciate all the feedback. Please keep in mind that I am a complete novice when it comes to Java.

    I may not use the proper terminology in my explanations so please feel free to correct me when neccessary.

    Thanks

  • TPD-Opitz
    TPD-Opitz Dipl.-Ing, Dipl.-Inf GermanyMember Posts: 2,465 Silver Trophy

    any Buffered* output class must be flushed before closing. So just befor close() call flush().

    bye

    TPD

This discussion has been closed.