To read the article online, visit http://www.4GuysFromRolla.com/webtech/081199-2.shtml

Using Java & ASP to Grab Stock Quotes
By Ian Stallings


In the last article we made a basic hello world Java object that we could interact with using ASP. If you have not read that article I recommend you check it out before reading further. This time we're going to really dig in and build a cool object that will grab a string on another site using the URL.

Ok, First thing we do is create the .java source file. Hopefully this will help you pick up the Java syntax. For those of you that are familiar with Javascript it should be pretty easy. Please read the comments in the code to get a better understanding of Java.

The code for class is available in text format.

You then go to the command line again and compile the StringGrabber.java file just like we did the last one. Once that file is compiled place the StringGrabber.class file in the %Win%/Java/TrustLib directory. You can now access it from an ASP file.

Next, Let's create the ASP file using this VBscript:

<html>
<head>
</head>
<body>
<h2>StockGrabber Test</h2>

<%
Response.Write("<form action='stringgrabber.asp' method='post'>Stock Ticker Symbol:<br>")
Response.Write("<input type='text' name='ticker'><br>")
Response.Write("<input type='submit' value='submit'><br><br>")

If Request.Form("ticker") <> "" then
	strTicker = Request.Form("ticker")

	'Create a StringGrabber object.
	set StringGrabber = GetObject("java:StringGrabber")

	Response.Write(strTicker & GetStockPriceFromURL(strTicker)) 
End If


Function GetStockPriceFromURL(stockSymbol)

	preString = "<b>$"
	postString = "</b>"
	url = "http://quotes.nasdaq-amex.com/Quote.dll?page=multi&mode=Stock&symbol=" & stockSymbol

	stockPrice = StringGrabber.GetString(url, preString, postString)

	GetStockPriceFromURL = stockPrice
End Function
%>
</body>
</html>

This should go directly to Nasdaq's website and grab the newest stock price for the stock entered based on the ticker symbol. The string is found by parsing the web page and looking for the first <b>$ it finds. It then looks for the next </b> and take whatever's inbetween. This function can be altered greatly to get any string you want. Say the headlines from a news site, the weather, etc. You can change the web page it uses by altering the url variable.

Now let me make a note before I get anyone in trouble. Taking material from a site without permission can be seen as a copyright violation, So I don't advise taking any info from any site without permission. Especially Nasdaq. Unless you like tangling with the super-congolomo with extremely deep pockets type.

I have to give credit to Frank Leahy from Wired for his article on using IIS extensions in Java. Without his help I wouldn't have been able to write this article.

Happy Programming!


Attachments:

  • StringGrabber.java in text format
  • StringGrabber.asp in text format


    Related Links:

  • http://www.microsoft.com/java
  • http://www.gamelan.com
  • http://www.webmonkey.com/java



  • Article Information
    Article Title: Using Java & ASP to Grab Stock Quotes
    Article Author: Ian Stallings
    Published Date: Wednesday, August 11, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/081199-2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers