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" +
            "</html>";
    JEditorPane edit=new JEditorPane();
	
    public JEditorPanePost() {
        JFrame frame=new JFrame("Submit form tag method=POST in HTMLEditorKit");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new JScrollPane(edit));
 
        HTMLEditorKit kit=new HTMLEditorKit();
        edit.setEditorKit(kit);
        edit.setText(htmlText);
 
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
 
    public static void main(String[] args) throws Exception {
        new JEditorPanePost();
    }
 
}