Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Java Tip 34: POSTing via Java

Learn how to POST data to Web servers in Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
The plethora of browsers in use turns the relatively simple act of performing POST-style HTTP requests into a bit of a pain. In this tip we shall provide the recipe for making POSTs simple again.

Everybody using the Web is at least passingly familiar with POST HTTP request. POST requests are used to send out things like HTML form data from a Web page to a Web server. A good example of a POST form is the feedback form at the bottom of this page. The browser sends the form data as part of the POST request, and the Web server sends back a response (which is usually in the form of another Web page).

The POST request, however, does not have to be used with an HTML form. It is just another HTTP request/response protocol. We can use it for whatever nefarious (or legitimate :-) deeds we care to. For instance, we can use POSTs to a CGI-bin script on a Web server rather than a raw socket to a database server. The advantage is you don't have to worry about some users not being able to use your applet because they happen to sit behind a firewall that refuses to allow random socket connections to the outside world.

You can see the results of invoking the Web server's CGI-bin script directly. Here is the Java POSTing applet in action:

The results of the Java applet should be here.

The code for dealing with posts is similar to the code for the Java application we developed for Java Tip #19 to copy files from Web sites. The core differences have to do with the security restrictions placed on applets.

    URL                 url;
    URLConnection   urlConn;
    DataOutputStream    printout;
    DataInputStream     input;
    // URL of CGI-Bin script.
    url = new URL (getCodeBase().toString() + "env.tcgi");
    // URL connection channel.
    urlConn = url.openConnection();
    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);
    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);
    // No caching, we want the real thing.
    urlConn.setUseCaches (false);
    // Specify the content type.
    urlConn.setRequestProperty
    ("Content-Type", "application/x-www-form-urlencoded");
    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());
    String content =
    "name=" + URLEncoder.encode ("Buford Early") +
    "&email=" + URLEncoder.encode ("buford@known-space.com");
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();
    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());
    String str;
    while (null != ((str = input.readLine())))
    {
    System.out.println (str);
    textArea.appendText (str + "\n");
    }
    input.close ();


First we build the URL to the CGI-bin script on the server. We use getCodeBase() to give us the URL from which the applet was delivered. This is important since the (untrusted) applet is only allowed to contact the host from which it came.

After opening the URL connection, we have to tell the runtime system explicitly what we are going to be doing. We want to both send and receive data on this connection so we set both the input and output flags. We then turn off the use of caching since we want to make sure that the data we read back from the server is actually in response to the data that we send it. Finally, we set the HTTP request's "content-type" field to be "application/x-www-form-urlencoded." That is necessary since some version of Netscape's browser has a bug which does not give out this proper content type when doing POSTs.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (8)
Login
Forgot your account info?

I am Feeling LuckyBy Anonymous on January 10, 2010, 5:33 amYes, Its true, I am really feeling lucky. I googled for what here is, and i got exactly what i want Thank you....

Reply | Read entire comment

/* * Returns the number of business days, excluding the SAturdBy Anonymous on November 6, 2009, 5:09 am/* * Returns the number of business days, excluding the SAturday's and Sunday's. * With the arguments Request Expiration date and current days. * */ public...

Reply | Read entire comment

It works!By Anonymous on October 13, 2009, 12:33 pmThanks! the code here: http://www.exampledepot.com/egs/java.net/Post.html didn't help me...

Reply | Read entire comment

Hey, i did the same...But it unable to call the servlet...By Anonymous on October 8, 2009, 6:06 amHey, i did the same...But it unable to call the servlet. I tested the servlet alone,its working fine.But when i call the servlet from the applet,its not. Please...

Reply | Read entire comment

Excellent ArticleBy Anonymous on September 25, 2009, 12:45 pmThis is for what me searching

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.