Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 41: POSTing via Java revisited

Learn how to display the HTML document returned by the Web server

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 4

The server part is the most complicated piece, so let's look at the applet first. There are only a few differences between this applet and the Happy applet in the previous Java Tip 34. POSTing to the server is identical, but we have to modify the reading of the response from the server:

        input = new DataInputStream (urlConn.getInputStream ());
        String str = null;
        String firstLine = null;
        while (null != ((str = input.readLine())))
        {
        if (null == firstLine)
            firstLine = str;
        System.out.println (str);
        textArea.appendText (str + "\n");
        }
        input.close ();


By fiat, the server returns a magic key as the first line. The magic key is the piece of state information that is used to uniquely identify which transaction the applet is involved in with this server. If there are any problems in processing the POST request, the server notifies the applet that this is the case by returning the string "nil" followed by a textual description of the problem. The only thing the applet needs to do now is build the URL and call showDocument() to display the HTML:

        if (null != firstLine)
        {
        url = new URL ("http://" +
                   ((getCodeBase()).getHost()).toString() +
                   "/poster?" + firstLine);
        (getAppletContext()).showDocument (url, "_blank");
        }


Be sure to note that the URL parameter must be URL-encoded. In that snippet, we only have to add the question mark to separate the base URL from the passed parameters, since the magic key from the server is already safely encoded.

Now that we have the applet part taken care of, let's dive into the server. The server-side code in the earlier Java Tip on POSTing was a traditional CGI-bin script written in Perl. Perl is a fine solution, but wouldn't you rather write server-side Java? We can write CGI-bin scripts in Java (see the Resources section), but there's a much better solution: Java that is run as part of the Web server itself. This type of server-side Java is known as a servlet. The solution we're presenting here will be a servlet written to the Java Servlet API -- although you can implement the same solution using CGI-bin scripts in Perl, Tcl, Java, or whatever.

Note that an introduction to servlet programming and administration is beyond the scope of this article; we'll be covering only the main issues that directly relate to the POSTing solution.

The PosterServlet code contains a fair number of comments to guide your walk-through. There's lots of error handling and extra checking to handle the plethora of possible problems, some denial-of-service attacks, and so on, but mostly you can ignore that stuff. (We'll discuss security issues in more depth later.) The servlet is written to the Java 1.1.x APIs (whereas the applet code is Java 1.0.2 code).

The doPost() method handles the POST request -- that is, it takes care of the first three server responsibilities: It generates an HTML document based on the POSTed information, saves the document to a temporary disk file, and returns a magic key to the applet that identifies the HTML document and is suitable for directly embedding into a subsequent GET request.

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

missing the resource file....By sony213002 on January 27, 2009, 2:28 amHello, You are missing the rresource file com.distinct.rpfs.view.utils.ArabicNumbers.properties, with out that file, this program is useless, can you please...

Reply | Read entire comment

Number to Arabic words ConvertionBy vinods on October 10, 2008, 4:38 am package com.distinct.rpfs.view.utils; import java.util.ResourceBundle; public class NumToArabicConverter { public NumToArabicConverter() { } ...

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.