Creating the Getting Started Application

Creating the Getting Started Application

The example application contains a Converter class and a Web component. For this example, we will create a top-level project source directory named gs/. All of the files in this example application are created in this directory.

1 The Converter Class

The Converter class used in the example application is used in conjunction with a JavaServer Pages page. The resulting application is a form that enables you to convert American dollars to Euros or Yen. The source code for the Converterclass is in the <JWDSP_HOME >/docs/examples/gs/ directory.

Coding the Converter Class

The Converter class for this example implements two methods, dollarToYenand yenToEuro. The source code for the Converter class follows.

import java.math.*; 

public class Converter {

    static BigDecimal yenRate = new BigDecimal("138.7800"); 
    static BigDecimal euroRate = new BigDecimal("0.0084"); 

   public static BigDecimal dollarToYen(BigDecimal dollars) {
      BigDecimal result = dollars.multiply(yenRate); 
      return result.setScale(2,BigDecimal.ROUND_UP); 

   public static BigDecimal yenToEuro(BigDecimal yen) {
      BigDecimal result = yen.multiply(euroRate); 
      return result.setScale(2,BigDecimal.ROUND_UP); 

   public Converter() {} 

2 The Web Client

The Web client is contained in the JSP page <JWDSP_HOME >/docs/tutorials/examples/gs/index.jsp. A JSP page is a text-based document that contains both static and dynamic content. The static content is the template data that can be expressed in any text-based format, such as HTML, WML, or XML. JSP elements construct the dynamic content.

Coding the Web Client

The JSP page, index.jsp, is used to create the form that will appear in the Web browser when the application client is running. This JSP page is a typical mixture of static HTML markup and JSP elements. If you have developed Web pages, you are probably familiar with the HTML document structure statements (<head>, <body>, and so on) and the HTML statements that create a form <form> and a menu <select>. The highlighted lines in the example contain the following types of JSP constructs:

  • Directives (<%@page ... %> ) import classes in the Converter class, and set the content type returned by the page.

  • Scriptlets (<% ... %> ) retrieve the value of the amount request parameter, convert it to a BigDecimal, and convert the value to Yen or Euro.

  • Expressions (<%= ... %> ) insert the value of the amount into the response.

The source code for index.jsp follows.

<%@ page import="Converter,java.math.*" % > 
<%@ page contentType="text/html; charset=ISO-8859-1" % > 

<body bgcolor="white"> 
<p>Enter an amount to convert:</p> 
<form method="get"> 
<input type="text" name="amount" size="25"> 
<input type="submit" value="Submit"> 
<input type="reset" value="Reset"> 
     String amount = request.getParameter("amount"); 
    if ( amount != null && amount.length() > 0 ) { 
        BigDecimal d = new BigDecimal (amount); 
    <p><%= amount %> dollars are 
        <%= Converter.dollarToYen(d) %>  Yen. 
    <p><%= amount %> Yen are 
        <%= Converter.yenToEuro(d) %>  Euro. 

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows