Intro to JavaServer Pages: Create a JSP site

Put the JSP syntax to work. Learn how to create a JSP Web page.

By John Zukowski

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.

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.

<title>Stock Quotes</title>
<%@ page errorPage="errorPage.jsp" %>

<form action="quote.jsp"
method="GET"> <p>Enter Symbol: <input size="20" name="symbol"><input
type="submit" value="Submit"></p>

if (request.getParameter("symbol") != null) {

<jsp:useBean id="quotes" scope="page" class="com.jguru.Quotes" />
<jsp:setProperty name="quotes" property="*" />

<table border="1">
<th align="left">Symbol</th>
<th align="left">Name</th>
<th align="left">Price</th>
<td><jsp:getProperty name="quotes" property="symbol" /></td>
<td><jsp:getProperty name="quotes" property="name" /></td>
<td><jsp:getProperty name="quotes" property="price" /></td>


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" %>

<title>Error Page</title>

<h1>Our Error Page</h1></font>

<!— Print Exception —>
We got ourselves an exception:
    <%= exception %>
<a href="quote.jsp">Restart</a>
The Quotes JavaBean uses a Yahoo resource to get the stock pricing. Save the source (below) in the file in the classes\com\jguru directory under the JSWDK installation directory. From there, compile it with the javac compiler from the JSDK.

package com.jguru;

import java.util.*;

public class Quotes {
  String symbol;
  String name;
  String price;
  public void setSymbol(String symbol) {
    this.symbol = symbol;
  public String getSymbol() {
    return symbol;
  public String getName() {
    return name;
  public String getPrice() {
    return price;
  private void getSymbolValue(String symbol) {
    String urlString =
    "" +
    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.
Once you've created the two JSP files, as well as created the JavaBean source file and compiled it, you can load up the quote.jsp file to see the results from http://localhost:8080/quote.jsp, assuming you haven't changed the JSWDK installation to use a different port. The page can definitely be prettied up, but it does what it needs to do functionally and is a good demonstration of JSP's capabilities. John Zukowski is a software guru and director of education at In addition, John is the author of several Java books and numerous Java-related technical articles.

Editor's Picks

Free Newsletters, In your Inbox