Java Q&A

by Kenneth Golomb and Thomas Sorgie









Example 1:



//URL encode the data to the x-www-form-urlencoded MIME format

String urlEncodedData = URLEncoder.encode(data);

System.out.println("URL Encoded Data: " + urlEncodedData);



//Instance the URL (protocol, host, port)

String protocol = getCodeBase().getProtocol();

int  port = getCodeBase().getPort();

String host = getCodeBase().getHost();

URL url = new URL(protocol, host, port, "/servlet/ProxyServlet");

System.out.println("Destination URL: " + url.toString());



//Open the connection

urlConnection = url.openConnection();



//Turn off the caching feature

urlConnection.setUseCaches(false);



//Set the URL to POST instead of GET

urlConnection.setDoOutput(true);





Listing One

/* ClientExample.java -- @version 1.0

 * @author American Management Systems

 * Opens a connection to the specified URL using the HTTPS protocol

 * sends data that is passed as a parameter then reads the server response

 */



import java.applet.*;

import java.net.*;

import java.io.*;



public class ClientExample extends Applet {



   //The instance of the url connection

   private URLConnection urlConnection;



   /** Starts the applet this is called every time the page is reloaded */

   public void start() {

      System.out.println("ClientExample sends data to a 

                                               server using HTTPS POST");

      String data = getParameter("Data");



      send(data);

      read();

   }

   /** This is called to send the data over a new URL */

   public void send(String data) {

      try {

         System.out.println("Data: " + data);



         //URL encode the data to the x-www-form-urlencoded MIME format

         String urlEncodedData = URLEncoder.encode(data);

         System.out.println("URL Encoded Data: " + urlEncodedData);



         //Instance the URL (protocol, host, port)

         String protocol = getCodeBase().getProtocol();

         int  port = getCodeBase().getPort();

         String host = getCodeBase().getHost();

         URL url = new URL(protocol, host, port, "/servlet/ProxyServlet");

         System.out.println("Destination URL: " + url.toString());



         //Open the connection

         urlConnection = url.openConnection();



         //Turn off the caching feature

         urlConnection.setUseCaches(false);



         //Set the URL to POST instead of GET

         urlConnection.setDoOutput(true);



         //Set necessary properties

         urlConnection.setRequestProperty("content-type", 

                                    "application/x-www-form-urlencoded");

         urlConnection.setRequestProperty("content-length", 

                                String.valueOf(urlEncodedData.length()));



         //Get the output stream and send the data

         PrintStream outStream = 

                         new PrintStream(urlConnection.getOutputStream());

         outStream.println(urlEncodedData);

         outStream.close();

         System.out.println("Data sent successfully");

      }

         catch(Exception e) {

            System.out.println("URL Connection error: " + e.toString());

         }

    }



   /** This is called to read the response from the server */

   public void read() {

      try {

         //Get the input stream from the connection object

         DataInputStream in = 

                     new DataInputStream(new 

                     BufferedInputStream(urlConnection.getInputStream()));

         //Read a line of data

         String response = in.readLine();

         System.out.println("Server response: " + response);

      }

         catch(IOException e) {

           System.out.println("Error reading from input stream " 

                                                           + e.toString());

         }

   }

}



Listing Two

<html>

<head>

<title>ClientExample</title>

</head>



<body>

Check to the java console for output

<hr>



<applet code=ClientExample.class width=40 height=40>

<param name=Data value=ClientExample data>

</applet>



<hr>

</body>

</html>



Listing Three

servlet.ProxyServlet.code=ProxyServlet

servlet.ProxyServlet.initArgs=serverPort=4321,serverAddress=127.0.0.1







3



