Java -> web browser
Hi,
I have an applet that POSTs a request to a browser and then retrieves the servers response to check it was posted OK.
What I now need to do is stream the response back to the web browser to show the page that the server has returned. My code below works in that it reads the server response and outputs it to the console.
However I am confused as to how to hook back in to the browser ?
Any help would be really appreciated !
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
publicclass TestAppletextends JApplet
{
publicvoid init()
{
try
{
postResult("Hello server!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
privatevoid postResult(String message)throws MalformedURLException, IOException
{
String aLine;// only if reading response
URLurl;
URLConnectionurlConn;
DataOutputStreamprintout;
BufferedReaderinput;
// URL of CGI-Bin script.
url =new URL (getCodeBase().toString() +"test.php");
urlConn = url.openConnection();
// Let the run-time system (RTS) know what comms we want to make.
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
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 ="msg=" + URLEncoder.encode(message,"UTF-8");
printout.writeBytes (content);
printout.flush ();
printout.close ();
// Get response data. This is the bit that retrieves the page back from the server ...
input =new BufferedReader(new InputStreamReader(urlConn.getInputStream ()));
String str;
while (null != ((str = input.readLine())))
{
System.out.println (str);// <-- Need this to hook back in to browser...
}
input.close ();
}
}

