Skip to content
Advertisement

Automatically generate HTML pages in Java

I am developing a Java desktop application. I have a need to create HTML pages through my application. When the user clicks on View in Browser button a HTML page should be created with some details and shown it to the user.

Is there a way I can do this? Is there are any resources I can use in this situation?

Any suggestions are warmly welcome.

Advertisement

Answer

import java.awt.Desktop;
import java.io.*;

class ShowGeneratedHtml {

    public static void main(String[] args) throws Exception {
        File f = new File("source.htm");
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("<html><body><h1>Blah, Blah!</h1>");
        bw.write("<textarea cols=75 rows=10>");
        for (int ii=0; ii<20; ii++) {
            bw.write("Blah blah..");
        }
        bw.write("</textarea>");
        bw.write("</body></html>");
        bw.close();

        Desktop.getDesktop().browse(f.toURI());
    }
}

Result on this PC

enter image description here

Advertisement