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.

7
  • What should the html page contain? Commented Jun 13, 2011 at 6:31
  • Some images, tables and texts. Commented Jun 13, 2011 at 6:33
  • Is there a web server to show these html pages ? Is there a FTP/SSH connexion or something like that with it ? Commented Jun 13, 2011 at 6:39
  • 2
    @user754218, see if this link helps .. stackoverflow.com/questions/2323110/… Commented Jun 13, 2011 at 6: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. Commented Jun 13, 2011 at 6:41

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.

Comments

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.