Intro to JavaServer Pages: Create a JSP site
Takeaway: Put the JSP syntax to work. Learn how to create a JSP Web page.
By John Zukowski(2/8/00)
It's now time to put all this JSP syntax to use. We'll create a Web page that has an input form for a user to enter in a stock symbol and get the current stock price (delayed by 20 minutes). If there is an error, an error page is shown instead.
quote.jsp
First, create a quote.jsp page with the code below and save it in the Web pages directory under the JSWDK installation directory. Most of the page is standard HTML with JSP code interspersed. Line 6 is a page directive that says to send any error to the errorPage.jsp file. Lines 13 through 15 are a scriptlet that basically says only show the table if the "symbol" parameter is present. This if-block is closed on lines 32 through 34. Line 17 defines the JavaBean to use, and line 18 loads its symbol property from the parameter. Lines 27 through 29 just display the bean properties. Other than the if-block, there is really no Java coding involved.
<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>
errorPage.jsp
Next, save the following JSP source to the file errorPage.jsp in the Web pages directory. The clue that this is an error page is the first line, which sets the page directive isErrorPage attribute to true. Whereas the prior page said where the error page is, this page says it is the error page. The only other JSP-specific code in the JSP file is the access to the implicit exception object. The page just displays its value:
<%@ 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>.
Quotes.java
The Quotes JavaBean uses a Yahoo resource to get the stock pricing. Save the Quotes.java source (below) in the quotes.java file in the classes\com\jguru directory under the JSWDK installation directory. From there, compile it with the javac compiler from the JSDK.
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);
}
}
}
![]() Your first JSP page. |
SponsoredWhite Papers, Webcasts, and Downloads
- QoS: What Is It? Why Do We Need It? Global Knowledge
- Using the Six Laws of Persuasion in Negotiations Global Knowledge
- BitLocker: Is It Really Secure? Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


Harnessing the power of waves
Planting solar gardens
Fill your car for $1.10 a gallon?
