Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 584 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 666 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How do you write an output in columns to an html table?

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.
Answers
-
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
-
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
-
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);
-
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
-
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
-
any Buffered* output class must be flushed before closing. So just befor close() call flush().
bye
TPD