<html> <head> <title>Stock Quotes</title> </head> <body> <%@ page errorPage="errorPage.jsp" %> <form action="quote.jsp" method="GET"> <p>Enter Symbol: <input size="20" name="symbol"><input type="submit" value="Submit"></p> </form> <% if (request.getParameter("symbol") != null) { %> <jsp:useBean id="quotes" scope="page" class="com.jguru.Quotes" /> <jsp:setProperty name="quotes" property="*" /> <table border="1"> <tr> <th align="left">Symbol</th> <th align="left">Name</th> <th align="left">Price</th> </tr> <tr> <td><jsp:getProperty name="quotes" property="symbol" /></td> <td><jsp:getProperty name="quotes" property="name" /></td> <td><jsp:getProperty name="quotes" property="price" /></td> </tr> </table> <% } %> </body> </html> |
| <%@ page isErrorPage="true" %> <html> <head> <title>Error Page</title> </head> <body> <h1>Our Error Page</h1></font> <!-- Print Exception --> We got ourselves an exception: <%= exception %> <a href="quote.jsp">Restart</a> </body> </html> |
| package com.jguru; 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); } } } |