How do you open web pages in Java?
Opening a web page with the default web browser is easy:
java.awt.Desktop.getDesktop().browse(theURI);
Embedding a browser is not so easy. JEditorPane
has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.
java program to open a web page in browser and post some data into the opened page
You will need a web driver for this. Look at Selenium
I am pasting some code from Getting started with Selenium
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
This example shows how you can open google.com and then type some search text and click the search button.
Related Topics
Why Java Classes Do Not Inherit Annotations from Implemented Interfaces
Quickly Read the Last Line of a Text File
Make Documentbuilder.Parse Ignore Dtd References
Java Thread Garbage Collected or Not
Java Sslhandshakeexception "No Cipher Suites in Common"
Why Do These Two Multiplication Operations Give Different Results
Error: Selection Does Not Contain a Main Type
In Java, How to Write a String Literal Without Having to Escape Quotes
Java Filewriter with Append Mode
Collections.Sort with Multiple Fields
Read Url to String in Few Lines of Java Code
Convert Latitude/Longitude Point to a Pixels (X,Y) on Mercator Projection
String Concatenation in Java - When to Use +, Stringbuilder and Concat