Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
456
Chapter 17 Using Applets As Servlet Front Ends
Listing 17.7 SendPost.java (continued)
      
      // Tell browser to allow me to send data to server.
      connection.setDoOutput(true);
      
      ByteArrayOutputStream byteStream =
        new ByteArrayOutputStream(512); // Grows if necessary
      // Stream that writes into buffer
      PrintWriter out = new PrintWriter(byteStream, true);
      String postData =
        "firstName=" + encodedValue(firstNameField) +
        "&lastName=" + encodedValue(lastNameField) +
        "&emailAddress=" + encodedValue(emailAddressField);
      
      // Write POST data into local buffer
      out.print(postData);
      out.flush(); // Flush since above used print, not println
      // POST requests are required to have Content Length
      String lengthString =
        String.valueOf(byteStream.size());
      connection.setRequestProperty
        ("Content Length", lengthString);
      
      // Netscape sets the Content Type to multipart/form data
      // by default. So, if you want to send regular form data,
      // you need to set it to
      // application/x www form urlencoded, which is the
      // default for Internet Explorer. If you send
      // serialized POST data with an ObjectOutputStream,
      // the Content Type is irrelevant, so you could
      // omit this step.
      connection.setRequestProperty
        ("Content Type", "application/x www form urlencoded");
      
      // Write POST data to real output stream
      byteStream.writeTo(connection.getOutputStream());
      BufferedReader in =
        new BufferedReader(new InputStreamReader
                             (connection.getInputStream()));
      String line;
      String linefeed = "\n";
      resultsArea.setText("");
      while((line = in.readLine()) != null) {
        resultsArea.append(line);
        resultsArea.append(linefeed);
      }
    } catch(IOException ioe) {
      // Print debug info in Java Console
      System.out.println("IOException: " + ioe);
    }
  }
Home page for this book: www.coreservlets.com; Home page for sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved