For those of you in SW409, consider the following
code, for getting a stock quote.
Write a servlet that asks the use for
for the symbol and then produces the
quote.
You will need a form and servlet code
for producing the answer.
Here is the Quote code:
import java.util.*;
import java.net.*;
import java.io.*;
public class Quotes {
String symbol;
String name;
String price;
public void setSymbol(String symbol) {
this.symbol = symbol;
getSymbolValue(symbol);
}
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
private void getSymbolValue(String symbol) {
String urlString =
"http://quote.yahoo.com/download/javasoft.beans?SYMBOLS="
+
symbol + "&format=nl";
try {
URL url = new URL(urlString);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
StringTokenizer tokenizer = new StringTokenizer(line,",");
name = tokenizer.nextToken();
name = name.substring(1, name.length()-2); // remove quotes
price = tokenizer.nextToken();
price = price.substring(1, price.length()-2); // remove quotes
} catch (IOException exception) {
System.err.println("IOException: " + exception);
}
}
}
|