Articles Projects Tips Downloads Contacts About

Form submitting (method=POST) in JEditorPane with HTMLEditorKit.

If we want to use JEditorPane as a limited browser we often need to submit form sending the form's data to server and obtain results. The example shows how to implement this. Two importnat notes: the form must have target and action attributes specified and all inputs used in the form must have name and id attributes.

Using the code below I succesfully catched POST request in my servlet.

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
public class JEditorPanePost {
    public static String htmlText="<html>\n" +
            "<body>\n<form method='POST' action='http://localhost:8080/myPostServlet' target='http://localhost:8080/myPostServlet'>" +
            "<INPUT id='tf1' name='tf1' value='test'>\n" +
            "<INPUT type='SUBMIT' value='My submit'> \n" +
            "</form>\n" +
            "</body>\n" +
    JEditorPane edit=new JEditorPane();
    public JEditorPanePost() {
        JFrame frame=new JFrame("Submit form tag method=POST in HTMLEditorKit");
        frame.getContentPane().add(new JScrollPane(edit));
        HTMLEditorKit kit=new HTMLEditorKit();
    public static void main(String[] args) throws Exception {
        new JEditorPanePost();