|
|
The purpose of these notes is to get to the bottom of what's needed for Homework Six.
Step One: An Auxiliary Class
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpMessage {
URL servlet = null;
String args = null;
public HttpMessage(URL servlet)
{
this.servlet = servlet;
}
public InputStream sendGetMessage() throws IOException
{
return sendGetMessage(null);
}
public InputStream sendGetMessage(Properties args) throws IOException
{
String argString = "";
if (args != null) { argString = "?" + toEncodedString(args); }
URL url = new URL(servlet.toExternalForm() + argString);
URLConnection con = url.openConnection();
con.setUseCaches(false);
return con.getInputStream();
}
public InputStream sendPostMessage() throws IOException {
return sendPostMessage(null);
}
public InputStream sendPostMessage(Properties args) throws IOException
{
String argString = "";
if (args != null) {
argString = toEncodedString(args);
}
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded"
);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argString);
out.flush();
out.close();
return con.getInputStream();
}
private String toEncodedString(Properties args)
{
StringBuffer buf = new StringBuffer();
Enumeration names = args.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = args.getProperty(name);
buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
if (names.hasMoreElements()) buf.append("&");
}
return buf.toString();
}
}
Step Two: The Applet Itself
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatApplet extends Applet implements Runnable {
TextArea text;
Label label;
TextField input;
Thread thread;
String user;
public void init() {
URL codebase = getCodeBase();
user = getParameter("user");
if (user == null) user = "anonymous";
text = new TextArea();
text.setEditable(false);
label = new Label("Type here: ");
input = new TextField();
input.setEditable(true);
setLayout(new BorderLayout());
add("Center", text);
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
panel.add("West", label);
panel.add("Center", input);
add("South", panel);
text.appendText("URL: " + codebase + "\n");
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
text.appendText(contactServer());
}
}
public boolean handleEvent(Event event) {
switch (event.id) {
case Event.ACTION_EVENT:
if (event.target == input) {
broadcastMessage(input.getText() + "\n");
input.setText("");
return true;
}
}
return false;
}
void broadcastMessage(String message) {
message = user + ": " + message;
try {
URL url =
new URL("http://burrowww.cs.indiana.edu:/examples/servlet/ChatServlet");
HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("message", message);
msg.sendPostMessage(props);
} catch (Exception ignored) { }
}
String contactServer() {
String nextMessage = null;
try {
URL servlet =
new URL("http://burrowww.cs.indiana.edu:/examples/servlet/ChatServlet");
HttpMessage msg = new HttpMessage(servlet);
InputStream in = msg.sendGetMessage();
DataInputStream data = new DataInputStream(new BufferedInputStream(in));
nextMessage = data.readLine();
} catch (Exception e) {
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) { }
}
return nextMessage + "\n";
}
}
Make sure you contact your server. That's the purpose of the
red