3

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KasunKP
  • 141
  • 2
  • 3
  • 12
  • What should the html page contain? – Kaj Jun 13 '11 at 06:31
  • Some images, tables and texts. – KasunKP Jun 13 '11 at 06:33
  • Is there a web server to show these html pages ? Is there a FTP/SSH connexion or something like that with it ? – Istao Jun 13 '11 at 06:39
  • 2
    @user754218, see if this link helps .. http://stackoverflow.com/questions/2323110/how-to-open-html-file-in-default-browser-from-java-swing-application – hanumant Jun 13 '11 at 06:41
  • No, I just want a way to show these information to the user. I need only html files so the user can view them in their browser. I have seen some application doing this. They create html pages as reports so users can view them. – KasunKP Jun 13 '11 at 06:41
  • Excuse me for the naive remark, but what is the problem exactly? Is it that you want a ready made DOM for your HTML to be able to use to generate the pages? If that is what you want, I can't help you there :) but if you just want to write a document, I don't see the issue and so should you :) – Andrea Raimondi Jun 13 '11 at 08:24
  • Similar question has been answered beautifully [here.](https://stackoverflow.com/questions/5936003/write-html-file-using-java) – Dharmik Patel Apr 09 '18 at 15:38

5 Answers5

6
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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You should use the package javax.swing.text.html.HTML. E.g., it has JEditorPane. It provides HTML 3.2 support. You should just to set the name of the URL, and the page will be displayed, if a network connection is available. See the example.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
user565447
  • 969
  • 3
  • 14
  • 29
0

Build html using any of these libraries like jsoup, wffweb, j2html, jwebutils etc.. (referred from SO) and write it to a temporary file then call Desktop.getDesktop().browse(file.toURI());

May be something like this if we use wffweb

Html html = new Html(null) {{
    new Head(this);
    new Body(this) {{
        new NoTag(this, "Hello World");
    }};
}};

File tempFile = File.createTempFile("temporary_html", "html", new File("/home/username/tmpdir"));
html.toOutputStream(new FileOutputStream(tempFile));
Desktop.getDesktop().browse(tempFile.toURI());
0

You can have a look at - javax.swing.text.html.HTML.Tag It provides some basic functionality. If it is not enough, you can think about using JavaServer Pages.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
0

The Flying Saucer library is a pure Java library for rendering XML, XHTML, and CSS 2.1 content. Started by a member of the Java Swing team.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154