Posting data from an applet to the server on the Mac


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Posting data from an applet to the server on the Mac

  1. #1
    David Priebe Guest

    Posting data from an applet to the server on the Mac


    Here's a cute one for anyone interested.

    I've been working on some applets for quite a while now and have recently
    run into a problem when trying to save data back to the server. When
    trying to save data posted from a Mac, it takes nearly a minute to
    save the data. So far this probelm has been identified on machines
    running MRJ 2.2.2 and 2.2.4 under MacOS 9.0.4.

    The problem occurrs when trying to create a PrintWriter object. Is it
    possible to do this by creating something other than the PrintWriter
    object? Has anyone else run into this little problem?

    Thanks for any help you can provide.

    The code for sending the data to the server is as follows:

    public static String postURL(String myURLString, String myDataString)
    {
    StringBuffer myURLContent = new StringBuffer();
    String errorText = "";
    boolean anError = false;

    try
    {
    // Create the connection to the URL.
    URL url = new URL(myURLString);
    URLConnection connection = url.openConnection();

    connection.setDoOutput(true);

    // Send the data.
    // The slowdown happens right here.
    PrintWriter out = new PrintWriter(connection.getOutputStream(),true);
    out.println(myDataString);
    out.close();

    // Get the reply.
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    while( ( inputLine = in.readLine()) != null )
    {
    myURLContent = myURLContent.append(inputLine).append('\n');
    }

    in.close();
    }
    catch( MalformedURLException x){
    errorText = x.toString();
    anError = true;
    }
    catch (UnknownServiceException usx){
    errorText = usx.toString();
    anError = true;
    }
    catch (IOException iox){
    errorText = iox.toString();
    anError = !errorText.endsWith("<null>");
    }

    if( anError ){
    myURLContent = new StringBuffer("Error> accessing url> \n").append(myURLString).append("\n").append(errorText);
    }
    return myURLContent.toString();
    }


  2. #2
    Paul Clapham Guest

    Re: Posting data from an applet to the server on the Mac

    I'd just use an OutputStreamWriter instead of a PrintWriter, because you
    aren't printing. It doesn't have a println() method, you'd have to use
    write() instead, but you don't need a newline character at the end anyway
    (or do you)? Anyway, it's not clear to me that you have actually figured
    out where the delay is. I think it's not unlikely that
    "connection.getOutputStream()" is the delay point. But that wouldn't be
    difficult to determine.

    PC2

    "David Priebe" <david-priebe@uiowa.edu> wrote in message
    news:3ae5f121$1@news.devx.com...
    >
    > Here's a cute one for anyone interested.
    >
    > I've been working on some applets for quite a while now and have recently
    > run into a problem when trying to save data back to the server. When
    > trying to save data posted from a Mac, it takes nearly a minute to
    > save the data. So far this probelm has been identified on machines
    > running MRJ 2.2.2 and 2.2.4 under MacOS 9.0.4.
    >
    > The problem occurrs when trying to create a PrintWriter object. Is it
    > possible to do this by creating something other than the PrintWriter
    > object? Has anyone else run into this little problem?
    >
    > Thanks for any help you can provide.
    >
    > The code for sending the data to the server is as follows:
    >
    > public static String postURL(String myURLString, String myDataString)
    > {
    > StringBuffer myURLContent = new StringBuffer();
    > String errorText = "";
    > boolean anError = false;
    >
    > try
    > {
    > // Create the connection to the URL.
    > URL url = new URL(myURLString);
    > URLConnection connection = url.openConnection();
    >
    > connection.setDoOutput(true);
    >
    > // Send the data.
    > // The slowdown happens right here.
    > PrintWriter out = new PrintWriter(connection.getOutputStream(),true);
    > out.println(myDataString);
    > out.close();
    >
    > // Get the reply.
    > BufferedReader in = new BufferedReader(new

    InputStreamReader(connection.getInputStream()));
    > String inputLine;
    > while( ( inputLine = in.readLine()) != null )
    >


    > myURLContent = myURLContent.append(inputLine).append('\n');
    > }
    >
    > in.close();
    > }
    > catch( MalformedURLException x){
    > errorText = x.toString();
    > anError = true;
    > }
    > catch (UnknownServiceException usx){
    > errorText = usx.toString();
    > anError = true;
    > }
    > catch (IOException iox){
    > errorText = iox.toString();
    > anError = !errorText.endsWith("<null>");
    > }
    >
    > if( anError ){
    > myURLContent = new StringBuffer("Error> accessing url>

    \n").append(myURLString).append("\n").append(errorText);
    > }
    > return myURLContent.toString();
    > }
    >




  3. #3
    Ruchi Dhar Guest

    Re: Posting data from an applet to the server on the Mac


    There are two possible improvements you can have one use BufferedWriter if
    you don't bother about internationalization use BufferedOutputStream. Second
    is don't close the outputstream just flush the stream. The \r\n at the end
    is necessary to finish the communication.

    Try like this

    String data = "my post content\r\n";
    BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream(),
    1024);
    out.write(data.toBytes());
    out.flush();

    "David Priebe" <david-priebe@uiowa.edu> wrote:
    >
    >Here's a cute one for anyone interested.
    >
    >I've been working on some applets for quite a while now and have recently
    >run into a problem when trying to save data back to the server. When
    >trying to save data posted from a Mac, it takes nearly a minute to
    >save the data. So far this probelm has been identified on machines
    >running MRJ 2.2.2 and 2.2.4 under MacOS 9.0.4.
    >
    >The problem occurrs when trying to create a PrintWriter object. Is it
    >possible to do this by creating something other than the PrintWriter
    >object? Has anyone else run into this little problem?
    >
    >Thanks for any help you can provide.
    >
    >The code for sending the data to the server is as follows:
    >
    > public static String postURL(String myURLString, String myDataString)
    > {
    > StringBuffer myURLContent = new StringBuffer();
    > String errorText = "";
    > boolean anError = false;
    >
    > try
    > {
    > // Create the connection to the URL.
    > URL url = new URL(myURLString);
    > URLConnection connection = url.openConnection();
    >
    > connection.setDoOutput(true);
    >
    > // Send the data.
    > // The slowdown happens right here.
    > PrintWriter out = new PrintWriter(connection.getOutputStream(),true);
    > out.println(myDataString);
    > out.close();
    >
    > // Get the reply.
    > BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    > String inputLine;
    > while( ( inputLine = in.readLine()) != null )
    > {
    > myURLContent = myURLContent.append(inputLine).append('\n');
    > }
    >
    > in.close();
    > }
    > catch( MalformedURLException x){
    > errorText = x.toString();
    > anError = true;
    > }
    > catch (UnknownServiceException usx){
    > errorText = usx.toString();
    > anError = true;
    > }
    > catch (IOException iox){
    > errorText = iox.toString();
    > anError = !errorText.endsWith("<null>");
    > }
    >
    > if( anError ){
    > myURLContent = new StringBuffer("Error> accessing url> \n").append(myURLString).append("\n").append(errorText);
    > }
    > return myURLContent.toString();
    > }
    >



  4. #4
    David Priebe Guest

    Re: Posting data from an applet to the server on the Mac


    Good call. Yes, it is getOutputStream. Why I didn't see that before
    I don't know. It's not clear to me if I need the newline or not. The
    books I have aren't too specific on how to indicate the end of a post
    request to a CGI. Since PrintWriter doesn't seem to be the problem,
    I don't see a reason to change it.

    You wouldn't happen to know of a work-around for _this_ problem
    would you?

    Thanks for your help.

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >I'd just use an OutputStreamWriter instead of a PrintWriter, because you
    >aren't printing. It doesn't have a println() method, you'd have to use
    >write() instead, but you don't need a newline character at the end anyway
    >(or do you)? Anyway, it's not clear to me that you have actually figured
    >out where the delay is. I think it's not unlikely that
    >"connection.getOutputStream()" is the delay point. But that wouldn't be
    >difficult to determine.
    >
    >PC2
    >
    >"David Priebe" <david-priebe@uiowa.edu> wrote in message
    >news:3ae5f121$1@news.devx.com...
    >>
    >> Here's a cute one for anyone interested.
    >>
    >> I've been working on some applets for quite a while now and have recently
    >> run into a problem when trying to save data back to the server. When
    >> trying to save data posted from a Mac, it takes nearly a minute to
    >> save the data. So far this probelm has been identified on machines
    >> running MRJ 2.2.2 and 2.2.4 under MacOS 9.0.4.
    >>
    >> The problem occurrs when trying to create a PrintWriter object. Is it
    >> possible to do this by creating something other than the PrintWriter
    >> object? Has anyone else run into this little problem?
    >>
    >> Thanks for any help you can provide.
    >>
    >> The code for sending the data to the server is as follows:
    >>
    >> public static String postURL(String myURLString, String myDataString)
    >> {
    >> StringBuffer myURLContent = new StringBuffer();
    >> String errorText = "";
    >> boolean anError = false;
    >>
    >> try
    >> {
    >> // Create the connection to the URL.
    >> URL url = new URL(myURLString);
    >> URLConnection connection = url.openConnection();
    >>
    >> connection.setDoOutput(true);
    >>
    >> // Send the data.
    >> // The slowdown happens right here.
    >> PrintWriter out = new PrintWriter(connection.getOutputStream(),true);
    >> out.println(myDataString);
    >> out.close();
    >>
    >> // Get the reply.
    >> BufferedReader in = new BufferedReader(new

    >InputStreamReader(connection.getInputStream()));
    >> String inputLine;
    >> while( ( inputLine = in.readLine()) != null )
    >>

    >
    >> myURLContent = myURLContent.append(inputLine).append('\n');
    >> }
    >>
    >> in.close();
    >> }
    >> catch( MalformedURLException x){
    >> errorText = x.toString();
    >> anError = true;
    >> }
    >> catch (UnknownServiceException usx){
    >> errorText = usx.toString();
    >> anError = true;
    >> }
    >> catch (IOException iox){
    >> errorText = iox.toString();
    >> anError = !errorText.endsWith("<null>");
    >> }
    >>
    >> if( anError ){
    >> myURLContent = new StringBuffer("Error> accessing url>

    >\n").append(myURLString).append("\n").append(errorText);
    >> }
    >> return myURLContent.toString();
    >> }
    >>

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center